]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/identity_conversion.rs
Merge branch 'macro-use' into HEAD
[rust.git] / clippy_lints / src / identity_conversion.rs
1 use rustc::lint::*;
2 use rustc::{declare_lint, lint_array};
3 use rustc::hir::*;
4 use syntax::ast::NodeId;
5 use crate::utils::{in_macro, match_def_path, match_trait_method, same_tys, snippet, span_lint_and_then};
6 use crate::utils::{opt_def_id, paths, resolve_node};
7
8 /// **What it does:** Checks for always-identical `Into`/`From` conversions.
9 ///
10 /// **Why is this bad?** Redundant code.
11 ///
12 /// **Known problems:** None.
13 ///
14 /// **Example:**
15 /// ```rust
16 /// // format!() returns a `String`
17 /// let s: String = format!("hello").into();
18 /// ```
19 declare_clippy_lint! {
20     pub IDENTITY_CONVERSION,
21     complexity,
22     "using always-identical `Into`/`From` conversions"
23 }
24
25 #[derive(Default)]
26 pub struct IdentityConversion {
27     try_desugar_arm: Vec<NodeId>,
28 }
29
30 impl LintPass for IdentityConversion {
31     fn get_lints(&self) -> LintArray {
32         lint_array!(IDENTITY_CONVERSION)
33     }
34 }
35
36 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for IdentityConversion {
37     fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
38         if in_macro(e.span) {
39             return;
40         }
41
42         if Some(&e.id) == self.try_desugar_arm.last() {
43             return;
44         }
45
46         match e.node {
47             ExprKind::Match(_, ref arms, MatchSource::TryDesugar) => {
48                 let e = match arms[0].body.node {
49                     ExprKind::Ret(Some(ref e)) | ExprKind::Break(_, Some(ref e)) => e,
50                     _ => return,
51                 };
52                 if let ExprKind::Call(_, ref args) = e.node {
53                     self.try_desugar_arm.push(args[0].id);
54                 } else {
55                     return;
56                 }
57             },
58
59             ExprKind::MethodCall(ref name, .., ref args) => {
60                 if match_trait_method(cx, e, &paths::INTO[..]) && &*name.ident.as_str() == "into" {
61                     let a = cx.tables.expr_ty(e);
62                     let b = cx.tables.expr_ty(&args[0]);
63                     if same_tys(cx, a, b) {
64                         let sugg = snippet(cx, args[0].span, "<expr>").into_owned();
65                         span_lint_and_then(cx, IDENTITY_CONVERSION, e.span, "identical conversion", |db| {
66                             db.span_suggestion(e.span, "consider removing `.into()`", sugg);
67                         });
68                     }
69                 }
70             },
71
72             ExprKind::Call(ref path, ref args) => if let ExprKind::Path(ref qpath) = path.node {
73                 if let Some(def_id) = opt_def_id(resolve_node(cx, qpath, path.hir_id)) {
74                     if match_def_path(cx.tcx, def_id, &paths::FROM_FROM[..]) {
75                         let a = cx.tables.expr_ty(e);
76                         let b = cx.tables.expr_ty(&args[0]);
77                         if same_tys(cx, a, b) {
78                             let sugg = snippet(cx, args[0].span, "<expr>").into_owned();
79                             let sugg_msg = format!("consider removing `{}()`", snippet(cx, path.span, "From::from"));
80                             span_lint_and_then(cx, IDENTITY_CONVERSION, e.span, "identical conversion", |db| {
81                                 db.span_suggestion(e.span, &sugg_msg, sugg);
82                             });
83                         }
84                     }
85                 }
86             },
87
88             _ => {},
89         }
90     }
91
92     fn check_expr_post(&mut self, _: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
93         if Some(&e.id) == self.try_desugar_arm.last() {
94             self.try_desugar_arm.pop();
95         }
96     }
97 }