2 use rustc::ty::TypeVariants::{TyRawPtr, TyRef};
4 use rustc_front::hir::*;
5 use utils::TRANSMUTE_PATH;
6 use utils::{match_def_path, snippet_opt, span_lint, span_lint_and_then};
8 /// **What it does:** This lint checks for transmutes to the original type of the object.
10 /// **Why is this bad?** Readability. The code tricks people into thinking that the original value was of some other type.
12 /// **Known problems:** None.
14 /// **Example:** `core::intrinsics::transmute(t)` where the result type is the same as `t`'s.
16 pub USELESS_TRANSMUTE,
18 "transmutes that have the same to and from types"
21 /// **What it does:*** This lint checks for transmutes between a type `T` and `*T`.
23 /// **Why is this bad?** It's easy to mistakenly transmute between a type and a pointer to that type.
25 /// **Known problems:** None.
27 /// **Example:** `core::intrinsics::transmute(t)` where the result type is the same as `*t` or `&t`'s.
29 pub CROSSPOINTER_TRANSMUTE,
31 "transmutes that have to or from types that are a pointer to the other"
34 /// **What it does:*** This lint checks for transmutes from a pointer to a reference.
36 /// **Why is this bad?** This can always be rewritten with `&` and `*`.
38 /// **Known problems:** None.
42 /// let _: &T = std::mem::transmute(p); // where p: *const T
43 /// // can be written:
47 pub TRANSMUTE_PTR_TO_REF,
49 "transmutes from a pointer to a reference type"
54 impl LintPass for Transmute {
55 fn get_lints(&self) -> LintArray {
57 CROSSPOINTER_TRANSMUTE,
64 impl LateLintPass for Transmute {
65 fn check_expr(&mut self, cx: &LateContext, e: &Expr) {
66 if let ExprCall(ref path_expr, ref args) = e.node {
67 if let ExprPath(None, _) = path_expr.node {
68 let def_id = cx.tcx.def_map.borrow()[&path_expr.id].def_id();
70 if match_def_path(cx, def_id, &TRANSMUTE_PATH) {
71 let from_ty = cx.tcx.expr_ty(&args[0]);
72 let to_ty = cx.tcx.expr_ty(e);
78 &format!("transmute from a type (`{}`) to itself", from_ty));
79 } else if is_ptr_to(to_ty, from_ty) {
81 CROSSPOINTER_TRANSMUTE,
83 &format!("transmute from a type (`{}`) to a pointer to that type (`{}`)", from_ty, to_ty));
84 } else if is_ptr_to(from_ty, to_ty) {
86 CROSSPOINTER_TRANSMUTE,
88 &format!("transmute from a type (`{}`) to the type that it points to (`{}`)", from_ty, to_ty));
90 check_ptr_to_ref(cx, from_ty, to_ty, e, &args[0]);
98 fn is_ptr_to(from: ty::Ty, to: ty::Ty) -> bool {
99 if let TyRawPtr(from_ptr) = from.sty {
106 fn check_ptr_to_ref<'tcx>(cx: &LateContext,
107 from_ty: ty::Ty<'tcx>,
109 e: &Expr, arg: &Expr) {
110 if let TyRawPtr(ref from_pty) = from_ty.sty {
111 if let TyRef(_, ref to_rty) = to_ty.sty {
112 let mess = format!("transmute from a pointer type (`{}`) to a reference type (`{}`)",
115 span_lint_and_then(cx, TRANSMUTE_PTR_TO_REF, e.span, &mess, |db| {
116 if let Some(arg) = snippet_opt(cx, arg.span) {
117 let (deref, cast) = if to_rty.mutbl == Mutability::MutMutable {
124 let sugg = if from_pty.ty == to_rty.ty {
125 format!("{}{}", deref, arg)
128 format!("{}({} as {} {})", deref, arg, cast, to_rty.ty)
131 db.span_suggestion(e.span, "try", sugg);