1 use crate::utils::sugg::Sugg;
3 get_parent_expr, is_type_diagnostic_item, match_def_path, match_trait_method, paths, snippet,
4 snippet_with_macro_callsite, span_lint_and_help, span_lint_and_sugg,
6 use if_chain::if_chain;
7 use rustc_errors::Applicability;
8 use rustc_hir::{Expr, ExprKind, HirId, MatchSource};
9 use rustc_lint::{LateContext, LateLintPass};
10 use rustc_middle::ty::{self, TyS};
11 use rustc_session::{declare_tool_lint, impl_lint_pass};
13 declare_clippy_lint! {
14 /// **What it does:** Checks for `Into`, `TryInto`, `From`, `TryFrom`,`IntoIter` calls
15 /// that useless converts to the same type as caller.
17 /// **Why is this bad?** Redundant code.
19 /// **Known problems:** None.
25 /// // format!() returns a `String`
26 /// let s: String = format!("hello").into();
29 /// let s: String = format!("hello");
31 pub USELESS_CONVERSION,
33 "calls to `Into`, `TryInto`, `From`, `TryFrom`, `IntoIter` that performs useless conversions to the same type"
37 pub struct UselessConversion {
38 try_desugar_arm: Vec<HirId>,
41 impl_lint_pass!(UselessConversion => [USELESS_CONVERSION]);
43 #[allow(clippy::too_many_lines)]
44 impl<'tcx> LateLintPass<'tcx> for UselessConversion {
45 fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) {
46 if e.span.from_expansion() {
50 if Some(&e.hir_id) == self.try_desugar_arm.last() {
55 ExprKind::Match(_, ref arms, MatchSource::TryDesugar) => {
56 let e = match arms[0].body.kind {
57 ExprKind::Ret(Some(ref e)) | ExprKind::Break(_, Some(ref e)) => e,
60 if let ExprKind::Call(_, ref args) = e.kind {
61 self.try_desugar_arm.push(args[0].hir_id);
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.typeck_results().expr_ty(e);
68 let b = cx.typeck_results().expr_ty(&args[0]);
69 if TyS::same_type(a, b) {
70 let sugg = snippet_with_macro_callsite(cx, args[0].span, "<expr>").to_string();
75 "useless conversion to the same type",
76 "consider removing `.into()`",
78 Applicability::MachineApplicable, // snippet
82 if match_trait_method(cx, e, &paths::INTO_ITERATOR) && &*name.ident.as_str() == "into_iter" {
83 if let Some(parent_expr) = get_parent_expr(cx, e) {
84 if let ExprKind::MethodCall(ref parent_name, ..) = parent_expr.kind {
85 if &*parent_name.ident.as_str() != "into_iter" {
90 let a = cx.typeck_results().expr_ty(e);
91 let b = cx.typeck_results().expr_ty(&args[0]);
92 if TyS::same_type(a, b) {
93 let sugg = snippet(cx, args[0].span, "<expr>").into_owned();
98 "useless conversion to the same type",
99 "consider removing `.into_iter()`",
101 Applicability::MachineApplicable, // snippet
105 if match_trait_method(cx, e, &paths::TRY_INTO_TRAIT) && &*name.ident.as_str() == "try_into" {
107 let a = cx.typeck_results().expr_ty(e);
108 let b = cx.typeck_results().expr_ty(&args[0]);
109 if is_type_diagnostic_item(cx, a, sym!(result_type));
110 if let ty::Adt(_, substs) = a.kind();
111 if let Some(a_type) = substs.types().next();
112 if TyS::same_type(a_type, b);
119 "useless conversion to the same type",
121 "consider removing `.try_into()`",
128 ExprKind::Call(ref path, ref args) => {
131 if let ExprKind::Path(ref qpath) = path.kind;
132 if let Some(def_id) = cx.qpath_res(qpath, path.hir_id).opt_def_id();
133 let a = cx.typeck_results().expr_ty(e);
134 let b = cx.typeck_results().expr_ty(&args[0]);
138 if match_def_path(cx, def_id, &paths::TRY_FROM);
139 if is_type_diagnostic_item(cx, a, sym!(result_type));
140 if let ty::Adt(_, substs) = a.kind();
141 if let Some(a_type) = substs.types().next();
142 if TyS::same_type(a_type, b);
145 let hint = format!("consider removing `{}()`", snippet(cx, path.span, "TryFrom::try_from"));
150 "useless conversion to the same type",
158 if match_def_path(cx, def_id, &paths::FROM_FROM);
159 if TyS::same_type(a, b);
162 let sugg = Sugg::hir_with_macro_callsite(cx, &args[0], "<expr>").maybe_par();
164 format!("consider removing `{}()`", snippet(cx, path.span, "From::from"));
169 "useless conversion to the same type",
172 Applicability::MachineApplicable, // snippet
184 fn check_expr_post(&mut self, _: &LateContext<'tcx>, e: &'tcx Expr<'_>) {
185 if Some(&e.hir_id) == self.try_desugar_arm.last() {
186 self.try_desugar_arm.pop();