1 use crate::source::snippet;
2 use crate::{get_pat_name, match_var};
3 use rustc_hir::intravisit::{walk_expr, NestedVisitorMap, Visitor};
4 use rustc_hir::{Body, BodyId, Expr, ExprKind, Param};
5 use rustc_lint::LateContext;
6 use rustc_middle::hir::map::Map;
7 use rustc_span::{Span, Symbol};
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 get_binding_name(&body.params[idx]).map_or_else(
19 |name| extract_clone_suggestions(cx, name, replacements, body),
26 fn extract_clone_suggestions<'tcx>(
27 cx: &LateContext<'tcx>,
29 replace: &[(&'static str, &'static str)],
31 ) -> Option<Vec<(Span, Cow<'static, str>)>> {
32 let mut visitor = PtrCloneVisitor {
39 visitor.visit_body(body);
40 if visitor.abort { None } else { Some(visitor.spans) }
43 struct PtrCloneVisitor<'a, 'tcx> {
44 cx: &'a LateContext<'tcx>,
46 replace: &'a [(&'static str, &'static str)],
47 spans: Vec<(Span, Cow<'static, str>)>,
51 impl<'a, 'tcx> Visitor<'tcx> for PtrCloneVisitor<'a, 'tcx> {
54 fn visit_expr(&mut self, expr: &'tcx Expr<'_>) {
58 if let ExprKind::MethodCall(ref seg, _, ref args, _) = expr.kind {
59 if args.len() == 1 && match_var(&args[0], self.name) {
60 if seg.ident.name.as_str() == "capacity" {
64 for &(fn_name, suffix) in self.replace {
65 if seg.ident.name.as_str() == fn_name {
67 .push((expr.span, snippet(self.cx, args[0].span, "_") + suffix));
73 walk_expr(self, expr);
76 fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
77 NestedVisitorMap::None
81 fn get_binding_name(arg: &Param<'_>) -> Option<Symbol> {
82 get_pat_name(&arg.pat)