summaryrefslogtreecommitdiffstats
path: root/src/tools/clippy/clippy_lints/src/methods/inefficient_to_string.rs
blob: e1c9b5248a8a4f912c434a6f1a8886d9e59b7dcd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::source::snippet_with_applicability;
use clippy_utils::ty::{is_type_diagnostic_item, walk_ptrs_ty_depth};
use clippy_utils::{match_def_path, paths};
use if_chain::if_chain;
use rustc_errors::Applicability;
use rustc_hir as hir;
use rustc_lint::LateContext;
use rustc_middle::ty::{self, Ty};
use rustc_span::symbol::{sym, Symbol};

use super::INEFFICIENT_TO_STRING;

/// Checks for the `INEFFICIENT_TO_STRING` lint
pub fn check<'tcx>(
    cx: &LateContext<'tcx>,
    expr: &hir::Expr<'_>,
    method_name: Symbol,
    receiver: &hir::Expr<'_>,
    args: &[hir::Expr<'_>],
) {
    if_chain! {
        if args.is_empty() && method_name == sym::to_string;
        if let Some(to_string_meth_did) = cx.typeck_results().type_dependent_def_id(expr.hir_id);
        if match_def_path(cx, to_string_meth_did, &paths::TO_STRING_METHOD);
        if let Some(substs) = cx.typeck_results().node_substs_opt(expr.hir_id);
        let arg_ty = cx.typeck_results().expr_ty_adjusted(receiver);
        let self_ty = substs.type_at(0);
        let (deref_self_ty, deref_count) = walk_ptrs_ty_depth(self_ty);
        if deref_count >= 1;
        if specializes_tostring(cx, deref_self_ty);
        then {
            span_lint_and_then(
                cx,
                INEFFICIENT_TO_STRING,
                expr.span,
                &format!("calling `to_string` on `{}`", arg_ty),
                |diag| {
                    diag.help(&format!(
                        "`{}` implements `ToString` through a slower blanket impl, but `{}` has a fast specialization of `ToString`",
                        self_ty, deref_self_ty
                    ));
                    let mut applicability = Applicability::MachineApplicable;
                    let arg_snippet = snippet_with_applicability(cx, receiver.span, "..", &mut applicability);
                    diag.span_suggestion(
                        expr.span,
                        "try dereferencing the receiver",
                        format!("({}{}).to_string()", "*".repeat(deref_count), arg_snippet),
                        applicability,
                    );
                },
            );
        }
    }
}

/// Returns whether `ty` specializes `ToString`.
/// Currently, these are `str`, `String`, and `Cow<'_, str>`.
fn specializes_tostring(cx: &LateContext<'_>, ty: Ty<'_>) -> bool {
    if let ty::Str = ty.kind() {
        return true;
    }

    if is_type_diagnostic_item(cx, ty, sym::String) {
        return true;
    }

    if let ty::Adt(adt, substs) = ty.kind() {
        match_def_path(cx, adt.did(), &paths::COW) && substs.type_at(1).is_str()
    } else {
        false
    }
}