2019-10-16 14:54:20 -05:00
|
|
|
use super::INEFFICIENT_TO_STRING;
|
2020-04-23 01:12:03 -05:00
|
|
|
use crate::utils::{
|
|
|
|
is_type_diagnostic_item, match_def_path, paths, snippet_with_applicability, span_lint_and_then, walk_ptrs_ty_depth,
|
|
|
|
};
|
2019-10-16 14:54:20 -05:00
|
|
|
use if_chain::if_chain;
|
|
|
|
use rustc_errors::Applicability;
|
2020-01-06 10:39:50 -06:00
|
|
|
use rustc_hir as hir;
|
2020-01-12 00:08:41 -06:00
|
|
|
use rustc_lint::LateContext;
|
2020-03-30 04:02:14 -05:00
|
|
|
use rustc_middle::ty::{self, Ty};
|
2019-10-16 14:54:20 -05:00
|
|
|
|
|
|
|
/// Checks for the `INEFFICIENT_TO_STRING` lint
|
2019-12-27 01:12:26 -06:00
|
|
|
pub fn lint<'tcx>(cx: &LateContext<'_, 'tcx>, expr: &hir::Expr<'_>, arg: &hir::Expr<'_>, arg_ty: Ty<'tcx>) {
|
2019-10-16 14:54:20 -05:00
|
|
|
if_chain! {
|
|
|
|
if let Some(to_string_meth_did) = cx.tables.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.tables.node_substs_opt(expr.hir_id);
|
|
|
|
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 01:08:00 -05:00
|
|
|
|diag| {
|
|
|
|
diag.help(&format!(
|
2019-10-17 18:11:51 -05:00
|
|
|
"`{}` implements `ToString` through a slower blanket impl, but `{}` has a fast specialization of `ToString`",
|
2019-10-16 14:54:20 -05:00
|
|
|
self_ty, deref_self_ty
|
|
|
|
));
|
|
|
|
let mut applicability = Applicability::MachineApplicable;
|
|
|
|
let arg_snippet = snippet_with_applicability(cx, arg.span, "..", &mut applicability);
|
2020-04-17 01:08:00 -05:00
|
|
|
diag.span_suggestion(
|
2019-10-16 14:54:20 -05: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>`.
|
|
|
|
fn specializes_tostring(cx: &LateContext<'_, '_>, ty: Ty<'_>) -> bool {
|
2020-04-23 01:09:42 -05:00
|
|
|
if let ty::Str = ty.kind {
|
2020-04-23 01:12:03 -05:00
|
|
|
return true;
|
2020-04-23 01:09:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if is_type_diagnostic_item(cx, ty, sym!(string_type)) {
|
2020-04-23 01:12:03 -05:00
|
|
|
return true;
|
2020-04-23 01:09:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if let ty::Adt(adt, substs) = ty.kind {
|
2020-04-23 01:12:03 -05:00
|
|
|
match_def_path(cx, adt.did, &paths::COW) && substs.type_at(1).is_str()
|
2020-04-23 01:09:42 -05:00
|
|
|
} else {
|
|
|
|
false
|
2019-10-16 14:54:20 -05:00
|
|
|
}
|
|
|
|
}
|