]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/identity_conversion.rs
Auto merge of #4082 - Manishearth:macro-check-split, r=oli-obk
[rust.git] / clippy_lints / src / identity_conversion.rs
1 use crate::utils::{
2     in_macro_or_desugar, 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, impl_lint_pass};
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_lint_pass!(IdentityConversion => [IDENTITY_CONVERSION]);
33
34 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for IdentityConversion {
35     fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
36         if in_macro_or_desugar(e.span) {
37             return;
38         }
39
40         if Some(&e.hir_id) == self.try_desugar_arm.last() {
41             return;
42         }
43
44         match e.node {
45             ExprKind::Match(_, ref arms, MatchSource::TryDesugar) => {
46                 let e = match arms[0].body.node {
47                     ExprKind::Ret(Some(ref e)) | ExprKind::Break(_, Some(ref e)) => e,
48                     _ => return,
49                 };
50                 if let ExprKind::Call(_, ref args) = e.node {
51                     self.try_desugar_arm.push(args[0].hir_id);
52                 } else {
53                     return;
54                 }
55             },
56
57             ExprKind::MethodCall(ref name, .., ref args) => {
58                 if match_trait_method(cx, e, &paths::INTO[..]) && &*name.ident.as_str() == "into" {
59                     let a = cx.tables.expr_ty(e);
60                     let b = cx.tables.expr_ty(&args[0]);
61                     if same_tys(cx, a, b) {
62                         let sugg = snippet_with_macro_callsite(cx, args[0].span, "<expr>").to_string();
63
64                         span_lint_and_then(cx, IDENTITY_CONVERSION, e.span, "identical conversion", |db| {
65                             db.span_suggestion(
66                                 e.span,
67                                 "consider removing `.into()`",
68                                 sugg,
69                                 Applicability::MachineApplicable, // snippet
70                             );
71                         });
72                     }
73                 }
74                 if match_trait_method(cx, e, &paths::INTO_ITERATOR) && &*name.ident.as_str() == "into_iter" {
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                         span_lint_and_then(cx, IDENTITY_CONVERSION, e.span, "identical conversion", |db| {
80                             db.span_suggestion(
81                                 e.span,
82                                 "consider removing `.into_iter()`",
83                                 sugg,
84                                 Applicability::MachineApplicable, // snippet
85                             );
86                         });
87                     }
88                 }
89             },
90
91             ExprKind::Call(ref path, ref args) => {
92                 if let ExprKind::Path(ref qpath) = path.node {
93                     if let Some(def_id) = resolve_node(cx, qpath, path.hir_id).opt_def_id() {
94                         if cx.match_def_path(def_id, &paths::FROM_FROM[..]) {
95                             let a = cx.tables.expr_ty(e);
96                             let b = cx.tables.expr_ty(&args[0]);
97                             if same_tys(cx, a, b) {
98                                 let sugg = snippet(cx, args[0].span.source_callsite(), "<expr>").into_owned();
99                                 let sugg_msg =
100                                     format!("consider removing `{}()`", snippet(cx, path.span, "From::from"));
101                                 span_lint_and_then(cx, IDENTITY_CONVERSION, e.span, "identical conversion", |db| {
102                                     db.span_suggestion(
103                                         e.span,
104                                         &sugg_msg,
105                                         sugg,
106                                         Applicability::MachineApplicable, // snippet
107                                     );
108                                 });
109                             }
110                         }
111                     }
112                 }
113             },
114
115             _ => {},
116         }
117     }
118
119     fn check_expr_post(&mut self, _: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
120         if Some(&e.hir_id) == self.try_desugar_arm.last() {
121             self.try_desugar_arm.pop();
122         }
123     }
124 }