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 that can't ever be correct on any architecture
9 /// **Why is this bad?** It's basically guaranteed to be undefined behaviour
11 /// **Known problems:** When accessing C, users might want to store pointer sized objects in `extradata` arguments to save an allocation.
13 /// **Example:** `let ptr: *const T = core::intrinsics::transmute('x')`.
17 "transmutes that are confusing at best, undefined behaviour at worst and always useless"
20 /// **What it does:** This lint checks for transmutes to the original type of the object and transmutes that could be a cast.
22 /// **Why is this bad?** Readability. The code tricks people into thinking that something complex is going on
24 /// **Known problems:** None.
26 /// **Example:** `core::intrinsics::transmute(t)` where the result type is the same as `t`'s.
28 pub USELESS_TRANSMUTE,
30 "transmutes that have the same to and from types or could be a cast/coercion"
33 /// **What it does:*** This lint checks for transmutes between a type `T` and `*T`.
35 /// **Why is this bad?** It's easy to mistakenly transmute between a type and a pointer to that type.
37 /// **Known problems:** None.
39 /// **Example:** `core::intrinsics::transmute(t)` where the result type is the same as `*t` or `&t`'s.
41 pub CROSSPOINTER_TRANSMUTE,
43 "transmutes that have to or from types that are a pointer to the other"
46 /// **What it does:*** This lint checks for transmutes from a pointer to a reference.
48 /// **Why is this bad?** This can always be rewritten with `&` and `*`.
50 /// **Known problems:** None.
54 /// let _: &T = std::mem::transmute(p); // where p: *const T
55 /// // can be written:
59 pub TRANSMUTE_PTR_TO_REF,
61 "transmutes from a pointer to a reference type"
66 impl LintPass for Transmute {
67 fn get_lints(&self) -> LintArray {
68 lint_array![CROSSPOINTER_TRANSMUTE, TRANSMUTE_PTR_TO_REF, USELESS_TRANSMUTE, WRONG_TRANSMUTE]
72 impl LateLintPass for Transmute {
73 fn check_expr(&mut self, cx: &LateContext, e: &Expr) {
74 if let ExprCall(ref path_expr, ref args) = e.node {
75 if let ExprPath(None, _) = path_expr.node {
76 let def_id = cx.tcx.expect_def(path_expr.id).def_id();
78 if match_def_path(cx, def_id, &paths::TRANSMUTE) {
79 let from_ty = cx.tcx.expr_ty(&args[0]);
80 let to_ty = cx.tcx.expr_ty(e);
82 match (&from_ty.sty, &to_ty.sty) {
83 _ if from_ty == to_ty => span_lint(
87 &format!("transmute from a type (`{}`) to itself", from_ty),
89 (&TyRef(_, rty), &TyRawPtr(ptr_ty)) => span_lint_and_then(
93 "transmute from a reference to a pointer",
95 if let Some(arg) = snippet_opt(cx, args[0].span) {
96 let sugg = if ptr_ty == rty {
97 format!("{} as {}", arg, to_ty)
99 format!("{} as {} as {}", arg, cx.tcx.mk_ptr(rty), to_ty)
102 db.span_suggestion(e.span, "try", sugg);
106 (&ty::TyInt(_), &TyRawPtr(_)) |
107 (&ty::TyUint(_), &TyRawPtr(_)) => span_lint_and_then(
111 "transmute from an integer to a pointer",
113 if let Some(arg) = snippet_opt(cx, args[0].span) {
114 db.span_suggestion(e.span, "try", format!("{} as {}", arg, to_ty));
118 (&ty::TyFloat(_), &TyRef(..)) |
119 (&ty::TyFloat(_), &TyRawPtr(_)) |
120 (&ty::TyChar, &TyRef(..)) |
121 (&ty::TyChar, &TyRawPtr(_)) => span_lint(
125 &format!("transmute from a `{}` to a pointer", from_ty),
127 (&TyRawPtr(from_ptr), _) if from_ptr.ty == to_ty => span_lint(
129 CROSSPOINTER_TRANSMUTE,
131 &format!("transmute from a type (`{}`) to the type that it points to (`{}`)",
135 (_, &TyRawPtr(to_ptr)) if to_ptr.ty == from_ty => span_lint(
137 CROSSPOINTER_TRANSMUTE,
139 &format!("transmute from a type (`{}`) to a pointer to that type (`{}`)",
143 (&TyRawPtr(from_pty), &TyRef(_, to_rty)) => span_lint_and_then(
145 TRANSMUTE_PTR_TO_REF,
147 &format!("transmute from a pointer type (`{}`) to a reference type (`{}`)",
151 if let Some(arg) = snippet_opt(cx, args[0].span) {
152 let (deref, cast) = if to_rty.mutbl == Mutability::MutMutable {
159 let sugg = if from_pty.ty == to_rty.ty {
160 // Put things in parentheses if they are more complex
162 ExprPath(..) | ExprCall(..) | ExprMethodCall(..) | ExprBlock(..) => {
163 format!("{}{}", deref, arg)
165 _ => format!("{}({})", deref, arg)
168 format!("{}({} as {} {})", deref, arg, cast, to_rty.ty)
171 db.span_suggestion(e.span, "try", sugg);