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