Cleanup calls to layout_of

This commit is contained in:
Oliver Schneider 2018-02-05 11:28:09 +01:00
parent ce47e529d2
commit 503a63390d
No known key found for this signature in database
GPG key ID: A69F8D225B3AD7D9
2 changed files with 6 additions and 13 deletions

View file

@ -1,10 +1,10 @@
use rustc::lint::*;
use rustc::ty::{self, Ty};
use rustc::hir::*;
use rustc::ty::layout::LayoutOf;
use std::borrow::Cow;
use syntax::ast;
use utils::{last_path_segment, match_def_path, paths, snippet, span_lint, span_lint_and_then,
alignment};
use utils::{last_path_segment, match_def_path, paths, snippet, span_lint, span_lint_and_then};
use utils::{opt_def_id, sugg};
/// **What it does:** Checks for transmutes that can't ever be correct on any
@ -220,8 +220,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Transmute {
e.span,
&format!("transmute from a type (`{}`) to itself", from_ty),
),
_ if alignment(cx, from_ty).map(|a| a.abi())
< alignment(cx, to_ty).map(|a| a.abi())
_ if cx.layout_of(from_ty).ok().map(|a| a.align.abi())
< cx.layout_of(to_ty).ok().map(|a| a.align.abi())
=> span_lint(
cx,
MISALIGNED_TRANSMUTE,

View file

@ -9,7 +9,7 @@ use rustc::lint::{LateContext, Level, Lint, LintContext};
use rustc::session::Session;
use rustc::traits;
use rustc::ty::{self, Ty, TyCtxt};
use rustc::ty::layout::Align;
use rustc::ty::layout::LayoutOf;
use rustc_errors;
use std::borrow::Cow;
use std::env;
@ -1041,7 +1041,7 @@ pub fn is_try(expr: &Expr) -> Option<&Expr> {
}
pub fn type_size<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, ty: Ty<'tcx>) -> Option<u64> {
cx.tcx.layout_of(cx.param_env.and(ty))
cx.layout_of(ty)
.ok()
.map(|layout| layout.size.bytes())
}
@ -1060,10 +1060,3 @@ pub fn get_arg_name(pat: &Pat) -> Option<ast::Name> {
_ => None,
}
}
/// Returns alignment for a type, or None if alignment is undefined
pub fn alignment<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, ty: Ty<'tcx>) -> Option<Align> {
cx.tcx.layout_of(cx.param_env.and(ty))
.ok()
.map(|layout| layout.align)
}