1 use crate::utils::{get_pat_name, match_var, snippet};
2 use rustc::hir::intravisit::{walk_expr, NestedVisitorMap, Visitor};
4 use rustc::lint::LateContext;
7 use syntax::source_map::Span;
10 cx: &LateContext<'_, '_>,
11 opt_body_id: Option<BodyId>,
13 replacements: &'static [(&'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.arguments[idx]).map_or_else(
18 |name| extract_clone_suggestions(cx, name, replacements, body),
25 fn extract_clone_suggestions<'a, 'tcx: 'a>(
26 cx: &LateContext<'a, 'tcx>,
28 replace: &'static [(&'static str, &'static str)],
30 ) -> Option<Vec<(Span, Cow<'static, str>)>> {
31 let mut visitor = PtrCloneVisitor {
38 visitor.visit_body(body);
46 struct PtrCloneVisitor<'a, 'tcx: 'a> {
47 cx: &'a LateContext<'a, 'tcx>,
49 replace: &'static [(&'static str, &'static str)],
50 spans: Vec<(Span, Cow<'static, str>)>,
54 impl<'a, 'tcx: 'a> Visitor<'tcx> for PtrCloneVisitor<'a, 'tcx> {
55 fn visit_expr(&mut self, expr: &'tcx Expr) {
59 if let ExprKind::MethodCall(ref seg, _, ref args) = expr.node {
60 if args.len() == 1 && match_var(&args[0], self.name) {
61 if seg.ident.name == "capacity" {
65 for &(fn_name, suffix) in self.replace {
66 if seg.ident.name == fn_name {
68 .push((expr.span, snippet(self.cx, args[0].span, "_") + suffix));
75 walk_expr(self, expr);
78 fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
79 NestedVisitorMap::None
83 fn get_binding_name(arg: &Arg) -> Option<Name> {
84 get_pat_name(&arg.pat)