]> git.lizzy.rs Git - rust.git/blob - src/transmute.rs
Merge pull request #523 from sanxiyn/escape-arg
[rust.git] / src / transmute.rs
1 use rustc::lint::*;
2 use rustc_front::hir::*;
3 use utils;
4
5 /// **What it does:** This lint checks for transmutes to the original type of the object. It is `Warn` by default.
6 ///
7 /// **Why is this bad?** Readability. The code tricks people into thinking that the original value was of some other type.
8 ///
9 /// **Known problems:** None.
10 ///
11 /// **Example:** `core::intrinsics::transmute(t)` where the result type is the same as `t`'s.
12 declare_lint! {
13     pub USELESS_TRANSMUTE,
14     Warn,
15     "transmutes that have the same to and from types"
16 }
17
18 pub struct UselessTransmute;
19
20 impl LintPass for UselessTransmute {
21     fn get_lints(&self) -> LintArray {
22         lint_array!(USELESS_TRANSMUTE)
23     }
24 }
25
26 impl LateLintPass for UselessTransmute {
27     fn check_expr(&mut self, cx: &LateContext, e: &Expr) {
28         if let ExprCall(ref path_expr, ref args) = e.node {
29             if let ExprPath(None, _) = path_expr.node {
30                 let def_id = cx.tcx.def_map.borrow()[&path_expr.id].def_id();
31
32                 if utils::match_def_path(cx, def_id, &["core", "intrinsics", "transmute"]) {
33                     let from_ty = cx.tcx.expr_ty(&args[0]);
34                     let to_ty = cx.tcx.expr_ty(e);
35
36                     if from_ty == to_ty {
37                         cx.span_lint(USELESS_TRANSMUTE,
38                                      e.span,
39                                      &format!("transmute from a type (`{}`) to itself", from_ty));
40                     }
41                 }
42             }
43         }
44     }
45 }