]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/methods/inefficient_to_string.rs
Auto merge of #96098 - JakobDegen:always-return-place, r=oli-obk
[rust.git] / clippy_lints / src / methods / inefficient_to_string.rs
1 use clippy_utils::diagnostics::span_lint_and_then;
2 use clippy_utils::source::snippet_with_applicability;
3 use clippy_utils::ty::{is_type_diagnostic_item, walk_ptrs_ty_depth};
4 use clippy_utils::{match_def_path, paths};
5 use if_chain::if_chain;
6 use rustc_errors::Applicability;
7 use rustc_hir as hir;
8 use rustc_lint::LateContext;
9 use rustc_middle::ty::{self, Ty};
10 use rustc_span::symbol::{sym, Symbol};
11
12 use super::INEFFICIENT_TO_STRING;
13
14 /// Checks for the `INEFFICIENT_TO_STRING` lint
15 pub fn check<'tcx>(cx: &LateContext<'tcx>, expr: &hir::Expr<'_>, method_name: Symbol, args: &[hir::Expr<'_>]) {
16     if_chain! {
17         if args.len() == 1 && method_name == sym!(to_string);
18         if let Some(to_string_meth_did) = cx.typeck_results().type_dependent_def_id(expr.hir_id);
19         if match_def_path(cx, to_string_meth_did, &paths::TO_STRING_METHOD);
20         if let Some(substs) = cx.typeck_results().node_substs_opt(expr.hir_id);
21         let arg_ty = cx.typeck_results().expr_ty_adjusted(&args[0]);
22         let self_ty = substs.type_at(0);
23         let (deref_self_ty, deref_count) = walk_ptrs_ty_depth(self_ty);
24         if deref_count >= 1;
25         if specializes_tostring(cx, deref_self_ty);
26         then {
27             span_lint_and_then(
28                 cx,
29                 INEFFICIENT_TO_STRING,
30                 expr.span,
31                 &format!("calling `to_string` on `{}`", arg_ty),
32                 |diag| {
33                     diag.help(&format!(
34                         "`{}` implements `ToString` through a slower blanket impl, but `{}` has a fast specialization of `ToString`",
35                         self_ty, deref_self_ty
36                     ));
37                     let mut applicability = Applicability::MachineApplicable;
38                     let arg_snippet = snippet_with_applicability(cx, args[0].span, "..", &mut applicability);
39                     diag.span_suggestion(
40                         expr.span,
41                         "try dereferencing the receiver",
42                         format!("({}{}).to_string()", "*".repeat(deref_count), arg_snippet),
43                         applicability,
44                     );
45                 },
46             );
47         }
48     }
49 }
50
51 /// Returns whether `ty` specializes `ToString`.
52 /// Currently, these are `str`, `String`, and `Cow<'_, str>`.
53 fn specializes_tostring(cx: &LateContext<'_>, ty: Ty<'_>) -> bool {
54     if let ty::Str = ty.kind() {
55         return true;
56     }
57
58     if is_type_diagnostic_item(cx, ty, sym::String) {
59         return true;
60     }
61
62     if let ty::Adt(adt, substs) = ty.kind() {
63         match_def_path(cx, adt.did(), &paths::COW) && substs.type_at(1).is_str()
64     } else {
65         false
66     }
67 }