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