1 use crate::source::snippet;
2 use crate::visitors::{for_each_expr, Descend};
3 use crate::{path_to_local_id, strip_pat_refs};
4 use core::ops::ControlFlow;
5 use rustc_hir::{Body, BodyId, ExprKind, HirId, PatKind};
6 use rustc_lint::LateContext;
12 opt_body_id: Option<BodyId>,
14 replacements: &[(&'static str, &'static str)],
15 ) -> Option<Vec<(Span, Cow<'static, str>)>> {
16 if let Some(body) = opt_body_id.map(|id| cx.tcx.hir().body(id)) {
17 if let PatKind::Binding(_, binding_id, _, _) = strip_pat_refs(body.params[idx].pat).kind {
18 extract_clone_suggestions(cx, binding_id, replacements, body)
27 fn extract_clone_suggestions<'tcx>(
28 cx: &LateContext<'tcx>,
30 replace: &[(&'static str, &'static str)],
32 ) -> Option<Vec<(Span, Cow<'static, str>)>> {
33 let mut spans = Vec::new();
34 for_each_expr(body, |e| {
35 if let ExprKind::MethodCall(seg, recv, [], _) = e.kind
36 && path_to_local_id(recv, id)
38 if seg.ident.as_str() == "capacity" {
39 return ControlFlow::Break(());
41 for &(fn_name, suffix) in replace {
42 if seg.ident.as_str() == fn_name {
43 spans.push((e.span, snippet(cx, recv.span, "_") + suffix));
44 return ControlFlow::Continue(Descend::No);
48 ControlFlow::Continue(Descend::Yes)