1 use clippy_utils::diagnostics::{span_lint_and_help, span_lint_and_sugg};
2 use clippy_utils::source::{snippet, snippet_with_macro_callsite};
3 use clippy_utils::sugg::Sugg;
4 use clippy_utils::ty::{is_type_diagnostic_item, same_type_and_consts};
5 use clippy_utils::{get_parent_expr, is_trait_method, match_def_path, paths};
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};
11 use rustc_session::{declare_tool_lint, impl_lint_pass};
14 declare_clippy_lint! {
16 /// Checks for `Into`, `TryInto`, `From`, `TryFrom`, or `IntoIter` calls
17 /// which uselessly convert to the same type.
19 /// ### Why is this bad?
24 /// // format!() returns a `String`
25 /// let s: String = format!("hello").into();
30 /// let s: String = format!("hello");
32 #[clippy::version = "1.45.0"]
33 pub USELESS_CONVERSION,
35 "calls to `Into`, `TryInto`, `From`, `TryFrom`, or `IntoIter` which perform useless conversions to the same type"
39 pub struct UselessConversion {
40 try_desugar_arm: Vec<HirId>,
43 impl_lint_pass!(UselessConversion => [USELESS_CONVERSION]);
45 #[expect(clippy::too_many_lines)]
46 impl<'tcx> LateLintPass<'tcx> for UselessConversion {
47 fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) {
48 if e.span.from_expansion() {
52 if Some(&e.hir_id) == self.try_desugar_arm.last() {
57 ExprKind::Match(_, arms, MatchSource::TryDesugar) => {
58 let e = match arms[0].body.kind {
59 ExprKind::Ret(Some(e)) | ExprKind::Break(_, Some(e)) => e,
62 if let ExprKind::Call(_, [arg, ..]) = e.kind {
63 self.try_desugar_arm.push(arg.hir_id);
67 ExprKind::MethodCall(name, recv, ..) => {
68 if is_trait_method(cx, e, sym::Into) && name.ident.as_str() == "into" {
69 let a = cx.typeck_results().expr_ty(e);
70 let b = cx.typeck_results().expr_ty(recv);
71 if same_type_and_consts(a, b) {
72 let sugg = snippet_with_macro_callsite(cx, recv.span, "<expr>").to_string();
77 &format!("useless conversion to the same type: `{}`", b),
78 "consider removing `.into()`",
80 Applicability::MachineApplicable, // snippet
84 if is_trait_method(cx, e, sym::IntoIterator) && name.ident.name == sym::into_iter {
85 if let Some(parent_expr) = get_parent_expr(cx, e) {
86 if let ExprKind::MethodCall(parent_name, ..) = parent_expr.kind {
87 if parent_name.ident.name != sym::into_iter {
92 let a = cx.typeck_results().expr_ty(e);
93 let b = cx.typeck_results().expr_ty(recv);
94 if same_type_and_consts(a, b) {
95 let sugg = snippet(cx, recv.span, "<expr>").into_owned();
100 &format!("useless conversion to the same type: `{}`", b),
101 "consider removing `.into_iter()`",
103 Applicability::MachineApplicable, // snippet
108 if is_trait_method(cx, e, sym::TryInto) && name.ident.name == sym::try_into;
109 let a = cx.typeck_results().expr_ty(e);
110 let b = cx.typeck_results().expr_ty(recv);
111 if is_type_diagnostic_item(cx, a, sym::Result);
112 if let ty::Adt(_, substs) = a.kind();
113 if let Some(a_type) = substs.types().next();
114 if same_type_and_consts(a_type, b);
121 &format!("useless conversion to the same type: `{}`", b),
123 "consider removing `.try_into()`",
129 ExprKind::Call(path, [arg]) => {
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();
134 let a = cx.typeck_results().expr_ty(e);
135 let b = cx.typeck_results().expr_ty(arg);
137 if match_def_path(cx, def_id, &paths::TRY_FROM);
138 if is_type_diagnostic_item(cx, a, sym::Result);
139 if let ty::Adt(_, substs) = a.kind();
140 if let Some(a_type) = substs.types().next();
141 if same_type_and_consts(a_type, b);
144 let hint = format!("consider removing `{}()`", snippet(cx, path.span, "TryFrom::try_from"));
149 &format!("useless conversion to the same type: `{}`", b),
157 if match_def_path(cx, def_id, &paths::FROM_FROM);
158 if same_type_and_consts(a, b);
161 let sugg = Sugg::hir_with_macro_callsite(cx, arg, "<expr>").maybe_par();
163 format!("consider removing `{}()`", snippet(cx, path.span, "From::from"));
168 &format!("useless conversion to the same type: `{}`", b),
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();