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