]> git.lizzy.rs Git - rust.git/blob - src/transmute.rs
Lint transmute from ptr to ref
[rust.git] / src / transmute.rs
1 use rustc::lint::*;
2 use rustc::ty::TypeVariants::{TyRawPtr, TyRef};
3 use rustc::ty;
4 use rustc_front::hir::*;
5 use utils::TRANSMUTE_PATH;
6 use utils::{match_def_path, snippet_opt, span_lint, span_lint_and_then};
7
8 /// **What it does:** This lint checks for transmutes to the original type of the object.
9 ///
10 /// **Why is this bad?** Readability. The code tricks people into thinking that the original value was of some other type.
11 ///
12 /// **Known problems:** None.
13 ///
14 /// **Example:** `core::intrinsics::transmute(t)` where the result type is the same as `t`'s.
15 declare_lint! {
16     pub USELESS_TRANSMUTE,
17     Warn,
18     "transmutes that have the same to and from types"
19 }
20
21 /// **What it does:*** This lint checks for transmutes between a type `T` and `*T`.
22 ///
23 /// **Why is this bad?** It's easy to mistakenly transmute between a type and a pointer to that type.
24 ///
25 /// **Known problems:** None.
26 ///
27 /// **Example:** `core::intrinsics::transmute(t)` where the result type is the same as `*t` or `&t`'s.
28 declare_lint! {
29     pub CROSSPOINTER_TRANSMUTE,
30     Warn,
31     "transmutes that have to or from types that are a pointer to the other"
32 }
33
34 /// **What it does:*** This lint checks for transmutes from a pointer to a reference.
35 ///
36 /// **Why is this bad?** This can always be rewritten with `&` and `*`.
37 ///
38 /// **Known problems:** None.
39 ///
40 /// **Example:**
41 /// ```rust
42 /// let _: &T = std::mem::transmute(p); // where p: *const T
43 /// // can be written:
44 /// let _: &T = &*p;
45 /// ```
46 declare_lint! {
47     pub TRANSMUTE_PTR_TO_REF,
48     Warn,
49     "transmutes from a pointer to a reference type"
50 }
51
52 pub struct Transmute;
53
54 impl LintPass for Transmute {
55     fn get_lints(&self) -> LintArray {
56         lint_array! [
57             CROSSPOINTER_TRANSMUTE,
58             TRANSMUTE_PTR_TO_REF,
59             USELESS_TRANSMUTE
60         ]
61     }
62 }
63
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();
69
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);
73
74                     if from_ty == to_ty {
75                         span_lint(cx,
76                                   USELESS_TRANSMUTE,
77                                   e.span,
78                                   &format!("transmute from a type (`{}`) to itself", from_ty));
79                     } else if is_ptr_to(to_ty, from_ty) {
80                         span_lint(cx,
81                                   CROSSPOINTER_TRANSMUTE,
82                                   e.span,
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) {
85                         span_lint(cx,
86                                   CROSSPOINTER_TRANSMUTE,
87                                   e.span,
88                                   &format!("transmute from a type (`{}`) to the type that it points to (`{}`)", from_ty, to_ty));
89                     } else {
90                         check_ptr_to_ref(cx, from_ty, to_ty, e, &args[0]);
91                     }
92                 }
93             }
94         }
95     }
96 }
97
98 fn is_ptr_to(from: ty::Ty, to: ty::Ty) -> bool {
99     if let TyRawPtr(from_ptr) = from.sty {
100         from_ptr.ty == to
101     } else {
102         false
103     }
104 }
105
106 fn check_ptr_to_ref<'tcx>(cx: &LateContext,
107                           from_ty: ty::Ty<'tcx>,
108                           to_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 (`{}`)",
113                                from_ty,
114                                to_ty);
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 {
118                         ("&mut *", "*mut")
119                     } else {
120                         ("&*", "*const")
121                     };
122
123
124                     let sugg = if from_pty.ty == to_rty.ty {
125                         format!("{}{}", deref, arg)
126                     }
127                     else {
128                         format!("{}({} as {} {})", deref, arg, cast, to_rty.ty)
129                     };
130
131                     db.span_suggestion(e.span, "try", sugg);
132                 }
133             });
134         }
135     }
136 }