2 get_parent_expr, is_type_diagnostic_item, match_def_path, match_trait_method, paths, snippet,
3 snippet_with_macro_callsite, span_lint_and_help, span_lint_and_sugg,
5 use if_chain::if_chain;
6 use rustc_errors::Applicability;
7 use rustc_hir::{Expr, ExprKind, HirId, MatchSource};
8 use rustc_lint::{LateContext, LateLintPass};
9 use rustc_middle::ty::{self, TyS};
10 use rustc_session::{declare_tool_lint, impl_lint_pass};
12 declare_clippy_lint! {
13 /// **What it does:** Checks for `Into`, `TryInto`, `From`, `TryFrom`,`IntoIter` calls
14 /// that useless converts to the same type as caller.
16 /// **Why is this bad?** Redundant code.
18 /// **Known problems:** None.
24 /// // format!() returns a `String`
25 /// let s: String = format!("hello").into();
28 /// let s: String = format!("hello");
30 pub USELESS_CONVERSION,
32 "calls to `Into`, `TryInto`, `From`, `TryFrom`, `IntoIter` that performs useless conversions to the same type"
36 pub struct UselessConversion {
37 try_desugar_arm: Vec<HirId>,
40 impl_lint_pass!(UselessConversion => [USELESS_CONVERSION]);
42 #[allow(clippy::too_many_lines)]
43 impl<'tcx> LateLintPass<'tcx> for UselessConversion {
44 fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) {
45 if e.span.from_expansion() {
49 if Some(&e.hir_id) == self.try_desugar_arm.last() {
54 ExprKind::Match(_, ref arms, MatchSource::TryDesugar) => {
55 let e = match arms[0].body.kind {
56 ExprKind::Ret(Some(ref e)) | ExprKind::Break(_, Some(ref e)) => e,
59 if let ExprKind::Call(_, ref args) = e.kind {
60 self.try_desugar_arm.push(args[0].hir_id);
64 ExprKind::MethodCall(ref name, .., ref args, _) => {
65 if match_trait_method(cx, e, &paths::INTO) && &*name.ident.as_str() == "into" {
66 let a = cx.typeck_results().expr_ty(e);
67 let b = cx.typeck_results().expr_ty(&args[0]);
68 if TyS::same_type(a, b) {
69 let sugg = snippet_with_macro_callsite(cx, args[0].span, "<expr>").to_string();
74 "useless conversion to the same type",
75 "consider removing `.into()`",
77 Applicability::MachineApplicable, // snippet
81 if match_trait_method(cx, e, &paths::INTO_ITERATOR) && &*name.ident.as_str() == "into_iter" {
82 if let Some(parent_expr) = get_parent_expr(cx, e) {
83 if let ExprKind::MethodCall(ref parent_name, ..) = parent_expr.kind {
84 if &*parent_name.ident.as_str() != "into_iter" {
89 let a = cx.typeck_results().expr_ty(e);
90 let b = cx.typeck_results().expr_ty(&args[0]);
91 if TyS::same_type(a, b) {
92 let sugg = snippet(cx, args[0].span, "<expr>").into_owned();
97 "useless conversion to the same type",
98 "consider removing `.into_iter()`",
100 Applicability::MachineApplicable, // snippet
104 if match_trait_method(cx, e, &paths::TRY_INTO_TRAIT) && &*name.ident.as_str() == "try_into" {
106 let a = cx.typeck_results().expr_ty(e);
107 let b = cx.typeck_results().expr_ty(&args[0]);
108 if is_type_diagnostic_item(cx, a, sym!(result_type));
109 if let ty::Adt(_, substs) = a.kind;
110 if let Some(a_type) = substs.types().next();
111 if TyS::same_type(a_type, b);
118 "useless conversion to the same type",
120 "consider removing `.try_into()`",
127 ExprKind::Call(ref path, ref args) => {
130 if let ExprKind::Path(ref qpath) = path.kind;
131 if let Some(def_id) = cx.qpath_res(qpath, path.hir_id).opt_def_id();
132 let a = cx.typeck_results().expr_ty(e);
133 let b = cx.typeck_results().expr_ty(&args[0]);
137 if match_def_path(cx, def_id, &paths::TRY_FROM);
138 if is_type_diagnostic_item(cx, a, sym!(result_type));
139 if let ty::Adt(_, substs) = a.kind;
140 if let Some(a_type) = substs.types().next();
141 if TyS::same_type(a_type, b);
144 let hint = format!("consider removing `{}()`", snippet(cx, path.span, "TryFrom::try_from"));
149 "useless conversion to the same type",
157 if match_def_path(cx, def_id, &paths::FROM_FROM);
158 if TyS::same_type(a, b);
161 let sugg = snippet(cx, args[0].span.source_callsite(), "<expr>").into_owned();
163 format!("consider removing `{}()`", snippet(cx, path.span, "From::from"));
168 "useless conversion to the same type",
171 Applicability::MachineApplicable, // snippet
183 fn check_expr_post(&mut self, _: &LateContext<'tcx>, e: &'tcx Expr<'_>) {
184 if Some(&e.hir_id) == self.try_desugar_arm.last() {
185 self.try_desugar_arm.pop();