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