]> git.lizzy.rs Git - rust.git/blob - src/transmute.rs
Merge pull request #452 from fhartwig/lifetime-false-positives
[rust.git] / src / transmute.rs
1 use rustc::lint::*;
2 use rustc_front::hir::*;
3 use utils;
4
5 declare_lint! {
6     pub USELESS_TRANSMUTE,
7     Warn,
8     "transmutes that have the same to and from types"
9 }
10
11 pub struct UselessTransmute;
12
13 impl LintPass for UselessTransmute {
14     fn get_lints(&self) -> LintArray {
15         lint_array!(USELESS_TRANSMUTE)
16     }
17 }
18
19 impl LateLintPass for UselessTransmute {
20     fn check_expr(&mut self, cx: &LateContext, e: &Expr) {
21         if let ExprCall(ref path_expr, ref args) = e.node {
22             if let ExprPath(None, _) = path_expr.node {
23                 let def_id = cx.tcx.def_map.borrow()[&path_expr.id].def_id();
24
25                 if utils::match_def_path(cx, def_id, &["core", "intrinsics", "transmute"]) {
26                     let from_ty = cx.tcx.expr_ty(&args[0]);
27                     let to_ty = cx.tcx.expr_ty(e);
28
29                     if from_ty == to_ty {
30                         cx.span_lint(USELESS_TRANSMUTE, e.span,
31                                      &format!("transmute from a type (`{}`) to itself", from_ty));
32                     }
33                 }
34             }
35         }
36     }
37 }