|
| 1 | +use super::INEFFICIENT_TO_STRING; |
| 2 | +use crate::utils::{match_def_path, paths, snippet_with_applicability, span_lint_and_then, walk_ptrs_ty_depth}; |
| 3 | +use if_chain::if_chain; |
| 4 | +use rustc::hir; |
| 5 | +use rustc::lint::LateContext; |
| 6 | +use rustc::ty::{self, Ty}; |
| 7 | +use rustc_errors::Applicability; |
| 8 | + |
| 9 | +/// Checks for the `INEFFICIENT_TO_STRING` lint |
| 10 | +pub fn lint<'tcx>(cx: &LateContext<'_, 'tcx>, expr: &hir::Expr, arg: &hir::Expr, arg_ty: Ty<'tcx>) { |
| 11 | + if_chain! { |
| 12 | + if let Some(to_string_meth_did) = cx.tables.type_dependent_def_id(expr.hir_id); |
| 13 | + if match_def_path(cx, to_string_meth_did, &paths::TO_STRING_METHOD); |
| 14 | + if let Some(substs) = cx.tables.node_substs_opt(expr.hir_id); |
| 15 | + let self_ty = substs.type_at(0); |
| 16 | + let (deref_self_ty, deref_count) = walk_ptrs_ty_depth(self_ty); |
| 17 | + if deref_count >= 1; |
| 18 | + if specializes_tostring(cx, deref_self_ty); |
| 19 | + then { |
| 20 | + span_lint_and_then( |
| 21 | + cx, |
| 22 | + INEFFICIENT_TO_STRING, |
| 23 | + expr.span, |
| 24 | + &format!("calling `to_string` on `{}`", arg_ty), |
| 25 | + |db| { |
| 26 | + db.help(&format!( |
| 27 | + "`{}` implements `ToString` through a slower blanket impl, but `{}` has a fast specialization of `ToString`", |
| 28 | + self_ty, deref_self_ty |
| 29 | + )); |
| 30 | + let mut applicability = Applicability::MachineApplicable; |
| 31 | + let arg_snippet = snippet_with_applicability(cx, arg.span, "..", &mut applicability); |
| 32 | + db.span_suggestion( |
| 33 | + expr.span, |
| 34 | + "try dereferencing the receiver", |
| 35 | + format!("({}{}).to_string()", "*".repeat(deref_count), arg_snippet), |
| 36 | + applicability, |
| 37 | + ); |
| 38 | + }, |
| 39 | + ); |
| 40 | + } |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +/// Returns whether `ty` specializes `ToString`. |
| 45 | +/// Currently, these are `str`, `String`, and `Cow<'_, str>`. |
| 46 | +fn specializes_tostring(cx: &LateContext<'_, '_>, ty: Ty<'_>) -> bool { |
| 47 | + match ty.kind { |
| 48 | + ty::Str => true, |
| 49 | + ty::Adt(adt, substs) => { |
| 50 | + match_def_path(cx, adt.did, &paths::STRING) |
| 51 | + || (match_def_path(cx, adt.did, &paths::COW) && substs.type_at(1).is_str()) |
| 52 | + }, |
| 53 | + _ => false, |
| 54 | + } |
| 55 | +} |
0 commit comments