2 use rustc::ty::TypeVariants::{TyRawPtr, TyRef};
5 use utils::{match_def_path, paths, span_lint, span_lint_and_then};
8 /// **What it does:** This lint checks for transmutes that can't ever be correct on any architecture
10 /// **Why is this bad?** It's basically guaranteed to be undefined behaviour
12 /// **Known problems:** When accessing C, users might want to store pointer sized objects in `extradata` arguments to save an allocation.
14 /// **Example:** `let ptr: *const T = core::intrinsics::transmute('x')`.
18 "transmutes that are confusing at best, undefined behaviour at worst and always useless"
21 /// **What it does:** This lint checks for transmutes to the original type of the object and transmutes that could be a cast.
23 /// **Why is this bad?** Readability. The code tricks people into thinking that something complex is going on
25 /// **Known problems:** None.
27 /// **Example:** `core::intrinsics::transmute(t)` where the result type is the same as `t`'s.
29 pub USELESS_TRANSMUTE,
31 "transmutes that have the same to and from types or could be a cast/coercion"
34 /// **What it does:*** This lint checks for transmutes between a type `T` and `*T`.
36 /// **Why is this bad?** It's easy to mistakenly transmute between a type and a pointer to that type.
38 /// **Known problems:** None.
40 /// **Example:** `core::intrinsics::transmute(t)` where the result type is the same as `*t` or `&t`'s.
42 pub CROSSPOINTER_TRANSMUTE,
44 "transmutes that have to or from types that are a pointer to the other"
47 /// **What it does:*** This lint checks for transmutes from a pointer to a reference.
49 /// **Why is this bad?** This can always be rewritten with `&` and `*`.
51 /// **Known problems:** None.
55 /// let _: &T = std::mem::transmute(p); // where p: *const T
56 /// // can be written:
60 pub TRANSMUTE_PTR_TO_REF,
62 "transmutes from a pointer to a reference type"
67 impl LintPass for Transmute {
68 fn get_lints(&self) -> LintArray {
69 lint_array![CROSSPOINTER_TRANSMUTE, TRANSMUTE_PTR_TO_REF, USELESS_TRANSMUTE, WRONG_TRANSMUTE]
73 impl LateLintPass for Transmute {
74 fn check_expr(&mut self, cx: &LateContext, e: &Expr) {
75 if let ExprCall(ref path_expr, ref args) = e.node {
76 if let ExprPath(None, _) = path_expr.node {
77 let def_id = cx.tcx.expect_def(path_expr.id).def_id();
79 if match_def_path(cx, def_id, &paths::TRANSMUTE) {
80 let from_ty = cx.tcx.expr_ty(&args[0]);
81 let to_ty = cx.tcx.expr_ty(e);
83 match (&from_ty.sty, &to_ty.sty) {
84 _ if from_ty == to_ty => span_lint(
88 &format!("transmute from a type (`{}`) to itself", from_ty),
90 (&TyRef(_, rty), &TyRawPtr(ptr_ty)) => span_lint_and_then(
94 "transmute from a reference to a pointer",
96 if let Some(arg) = sugg::Sugg::hir_opt(cx, &*args[0]) {
97 let sugg = if ptr_ty == rty {
98 arg.as_ty(&to_ty.to_string())
100 arg.as_ty(&format!("{} as {}", cx.tcx.mk_ptr(rty), to_ty))
103 db.span_suggestion(e.span, "try", sugg.to_string());
107 (&ty::TyInt(_), &TyRawPtr(_)) |
108 (&ty::TyUint(_), &TyRawPtr(_)) => span_lint_and_then(
112 "transmute from an integer to a pointer",
114 if let Some(arg) = sugg::Sugg::hir_opt(cx, &*args[0]) {
115 db.span_suggestion(e.span, "try", arg.as_ty(&to_ty.to_string()).to_string());
119 (&ty::TyFloat(_), &TyRef(..)) |
120 (&ty::TyFloat(_), &TyRawPtr(_)) |
121 (&ty::TyChar, &TyRef(..)) |
122 (&ty::TyChar, &TyRawPtr(_)) => span_lint(
126 &format!("transmute from a `{}` to a pointer", from_ty),
128 (&TyRawPtr(from_ptr), _) if from_ptr.ty == to_ty => span_lint(
130 CROSSPOINTER_TRANSMUTE,
132 &format!("transmute from a type (`{}`) to the type that it points to (`{}`)",
136 (_, &TyRawPtr(to_ptr)) if to_ptr.ty == from_ty => span_lint(
138 CROSSPOINTER_TRANSMUTE,
140 &format!("transmute from a type (`{}`) to a pointer to that type (`{}`)",
144 (&TyRawPtr(from_pty), &TyRef(_, to_rty)) => span_lint_and_then(
146 TRANSMUTE_PTR_TO_REF,
148 &format!("transmute from a pointer type (`{}`) to a reference type (`{}`)",
152 let arg = sugg::Sugg::hir(cx, &args[0], "..");
153 let (deref, cast) = if to_rty.mutbl == Mutability::MutMutable {
160 let arg = if from_pty.ty == to_rty.ty {
163 arg.as_ty(&format!("{} {}", cast, to_rty.ty))
166 db.span_suggestion(e.span, "try", sugg::make_unop(deref, arg).to_string());