2021-03-25 18:29:11 +00:00
|
|
|
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};
|
2019-10-16 19:54:20 +00:00
|
|
|
use if_chain::if_chain;
|
|
|
|
use rustc_errors::Applicability;
|
2020-01-06 16:39:50 +00:00
|
|
|
use rustc_hir as hir;
|
2020-01-12 06:08:41 +00:00
|
|
|
use rustc_lint::LateContext;
|
2020-03-30 09:02:14 +00:00
|
|
|
use rustc_middle::ty::{self, Ty};
|
2021-03-25 18:29:11 +00:00
|
|
|
use rustc_span::symbol::{sym, Symbol};
|
|
|
|
|
|
|
|
use super::INEFFICIENT_TO_STRING;
|
2019-10-16 19:54:20 +00:00
|
|
|
|
|
|
|
/// Checks for the `INEFFICIENT_TO_STRING` lint
|
2021-03-25 18:29:11 +00:00
|
|
|
pub fn check<'tcx>(cx: &LateContext<'tcx>, expr: &hir::Expr<'_>, method_name: Symbol, args: &[hir::Expr<'_>]) {
|
2019-10-16 19:54:20 +00:00
|
|
|
if_chain! {
|
2021-03-25 18:29:11 +00:00
|
|
|
if args.len() == 1 && method_name == sym!(to_string);
|
2020-07-17 08:47:04 +00:00
|
|
|
if let Some(to_string_meth_did) = cx.typeck_results().type_dependent_def_id(expr.hir_id);
|
2019-10-16 19:54:20 +00:00
|
|
|
if match_def_path(cx, to_string_meth_did, &paths::TO_STRING_METHOD);
|
2020-07-17 08:47:04 +00:00
|
|
|
if let Some(substs) = cx.typeck_results().node_substs_opt(expr.hir_id);
|
2021-03-25 18:29:11 +00:00
|
|
|
let arg_ty = cx.typeck_results().expr_ty_adjusted(&args[0]);
|
2019-10-16 19:54:20 +00:00
|
|
|
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),
|
2020-04-17 06:08:00 +00:00
|
|
|
|diag| {
|
|
|
|
diag.help(&format!(
|
2019-10-17 23:11:51 +00:00
|
|
|
"`{}` implements `ToString` through a slower blanket impl, but `{}` has a fast specialization of `ToString`",
|
2019-10-16 19:54:20 +00:00
|
|
|
self_ty, deref_self_ty
|
|
|
|
));
|
|
|
|
let mut applicability = Applicability::MachineApplicable;
|
2021-03-25 18:29:11 +00:00
|
|
|
let arg_snippet = snippet_with_applicability(cx, args[0].span, "..", &mut applicability);
|
2020-04-17 06:08:00 +00:00
|
|
|
diag.span_suggestion(
|
2019-10-16 19:54:20 +00:00
|
|
|
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>`.
|
2020-06-25 20:41:36 +00:00
|
|
|
fn specializes_tostring(cx: &LateContext<'_>, ty: Ty<'_>) -> bool {
|
2020-08-03 22:18:29 +00:00
|
|
|
if let ty::Str = ty.kind() {
|
2020-04-23 06:12:03 +00:00
|
|
|
return true;
|
2020-04-23 06:09:42 +00:00
|
|
|
}
|
|
|
|
|
2021-10-02 23:51:01 +00:00
|
|
|
if is_type_diagnostic_item(cx, ty, sym::String) {
|
2020-04-23 06:12:03 +00:00
|
|
|
return true;
|
2020-04-23 06:09:42 +00:00
|
|
|
}
|
|
|
|
|
2020-08-03 22:18:29 +00:00
|
|
|
if let ty::Adt(adt, substs) = ty.kind() {
|
2020-04-23 06:12:03 +00:00
|
|
|
match_def_path(cx, adt.did, &paths::COW) && substs.type_at(1).is_str()
|
2020-04-23 06:09:42 +00:00
|
|
|
} else {
|
|
|
|
false
|
2019-10-16 19:54:20 +00:00
|
|
|
}
|
|
|
|
}
|