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