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