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