2 use rustc::ty::TypeVariants::{TyRawPtr, TyRef};
5 use utils::{match_def_path, paths, snippet_opt, span_lint, span_lint_and_then};
7 /// **What it does:** This lint checks for transmutes to the original type of the object.
9 /// **Why is this bad?** Readability. The code tricks people into thinking that the original value was of some other type.
11 /// **Known problems:** None.
13 /// **Example:** `core::intrinsics::transmute(t)` where the result type is the same as `t`'s.
15 pub USELESS_TRANSMUTE,
17 "transmutes that have the same to and from types"
20 /// **What it does:*** This lint checks for transmutes between a type `T` and `*T`.
22 /// **Why is this bad?** It's easy to mistakenly transmute between a type and a pointer to that type.
24 /// **Known problems:** None.
26 /// **Example:** `core::intrinsics::transmute(t)` where the result type is the same as `*t` or `&t`'s.
28 pub CROSSPOINTER_TRANSMUTE,
30 "transmutes that have to or from types that are a pointer to the other"
33 /// **What it does:*** This lint checks for transmutes from a pointer to a reference.
35 /// **Why is this bad?** This can always be rewritten with `&` and `*`.
37 /// **Known problems:** None.
41 /// let _: &T = std::mem::transmute(p); // where p: *const T
42 /// // can be written:
46 pub TRANSMUTE_PTR_TO_REF,
48 "transmutes from a pointer to a reference type"
53 impl LintPass for Transmute {
54 fn get_lints(&self) -> LintArray {
55 lint_array![CROSSPOINTER_TRANSMUTE, TRANSMUTE_PTR_TO_REF, USELESS_TRANSMUTE]
59 impl LateLintPass for Transmute {
60 fn check_expr(&mut self, cx: &LateContext, e: &Expr) {
61 if let ExprCall(ref path_expr, ref args) = e.node {
62 if let ExprPath(None, _) = path_expr.node {
63 let def_id = cx.tcx.def_map.borrow()[&path_expr.id].def_id();
65 if match_def_path(cx, def_id, &paths::TRANSMUTE) {
66 let from_ty = cx.tcx.expr_ty(&args[0]);
67 let to_ty = cx.tcx.expr_ty(e);
73 &format!("transmute from a type (`{}`) to itself", from_ty));
74 } else if is_ptr_to(to_ty, from_ty) {
76 CROSSPOINTER_TRANSMUTE,
78 &format!("transmute from a type (`{}`) to a pointer to that type (`{}`)",
81 } else if is_ptr_to(from_ty, to_ty) {
83 CROSSPOINTER_TRANSMUTE,
85 &format!("transmute from a type (`{}`) to the type that it points to (`{}`)",
89 check_ptr_to_ref(cx, from_ty, to_ty, e, &args[0]);
97 fn is_ptr_to(from: ty::Ty, to: ty::Ty) -> bool {
98 if let TyRawPtr(from_ptr) = from.sty {
105 fn check_ptr_to_ref<'tcx>(cx: &LateContext, from_ty: ty::Ty<'tcx>, to_ty: ty::Ty<'tcx>, e: &Expr, arg: &Expr) {
106 if let TyRawPtr(ref from_pty) = from_ty.sty {
107 if let TyRef(_, ref to_rty) = to_ty.sty {
108 let mess = format!("transmute from a pointer type (`{}`) to a reference type (`{}`)",
111 span_lint_and_then(cx, TRANSMUTE_PTR_TO_REF, e.span, &mess, |db| {
112 if let Some(arg) = snippet_opt(cx, arg.span) {
113 let (deref, cast) = if to_rty.mutbl == Mutability::MutMutable {
120 let sugg = if from_pty.ty == to_rty.ty {
121 format!("{}{}", deref, arg)
123 format!("{}({} as {} {})", deref, arg, cast, to_rty.ty)
126 db.span_suggestion(e.span, "try", sugg);