1 use crate::{get_pat_name, match_var, snippet};
2 use rustc_hir::intravisit::{walk_expr, NestedVisitorMap, Visitor};
3 use rustc_hir::{Body, BodyId, Expr, ExprKind, Param};
4 use rustc_lint::LateContext;
5 use rustc_middle::hir::map::Map;
6 use rustc_span::{Span, Symbol};
11 opt_body_id: Option<BodyId>,
13 replacements: &[(&'static str, &'static str)],
14 ) -> Option<Vec<(Span, Cow<'static, str>)>> {
15 if let Some(body) = opt_body_id.map(|id| cx.tcx.hir().body(id)) {
16 get_binding_name(&body.params[idx]).map_or_else(
18 |name| extract_clone_suggestions(cx, name, replacements, body),
25 fn extract_clone_suggestions<'tcx>(
26 cx: &LateContext<'tcx>,
28 replace: &[(&'static str, &'static str)],
30 ) -> Option<Vec<(Span, Cow<'static, str>)>> {
31 let mut visitor = PtrCloneVisitor {
38 visitor.visit_body(body);
39 if visitor.abort { None } else { Some(visitor.spans) }
42 struct PtrCloneVisitor<'a, 'tcx> {
43 cx: &'a LateContext<'tcx>,
45 replace: &'a [(&'static str, &'static str)],
46 spans: Vec<(Span, Cow<'static, str>)>,
50 impl<'a, 'tcx> Visitor<'tcx> for PtrCloneVisitor<'a, 'tcx> {
53 fn visit_expr(&mut self, expr: &'tcx Expr<'_>) {
57 if let ExprKind::MethodCall(ref seg, _, ref args, _) = expr.kind {
58 if args.len() == 1 && match_var(&args[0], self.name) {
59 if seg.ident.name.as_str() == "capacity" {
63 for &(fn_name, suffix) in self.replace {
64 if seg.ident.name.as_str() == fn_name {
66 .push((expr.span, snippet(self.cx, args[0].span, "_") + suffix));
72 walk_expr(self, expr);
75 fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
76 NestedVisitorMap::None
80 fn get_binding_name(arg: &Param<'_>) -> Option<Symbol> {
81 get_pat_name(&arg.pat)