2019-05-14 08:06:21 +00:00
|
|
|
use crate::utils::{get_pat_name, match_var, snippet};
|
2018-12-29 15:04:45 +00:00
|
|
|
use rustc::hir::intravisit::{walk_expr, NestedVisitorMap, Visitor};
|
|
|
|
use rustc::hir::*;
|
|
|
|
use rustc::lint::LateContext;
|
2018-11-27 20:14:15 +00:00
|
|
|
use std::borrow::Cow;
|
2018-12-29 15:04:45 +00:00
|
|
|
use syntax::ast::Name;
|
|
|
|
use syntax::source_map::Span;
|
2017-10-08 08:51:44 +00:00
|
|
|
|
|
|
|
pub fn get_spans(
|
2018-07-23 11:01:12 +00:00
|
|
|
cx: &LateContext<'_, '_>,
|
2017-10-08 08:51:44 +00:00
|
|
|
opt_body_id: Option<BodyId>,
|
|
|
|
idx: usize,
|
2019-05-17 21:53:54 +00:00
|
|
|
replacements: &[(&'static str, &'static str)],
|
2017-10-08 08:51:44 +00:00
|
|
|
) -> Option<Vec<(Span, Cow<'static, str>)>> {
|
2018-12-08 00:56:03 +00:00
|
|
|
if let Some(body) = opt_body_id.map(|id| cx.tcx.hir().body(id)) {
|
2019-08-28 09:27:06 +00:00
|
|
|
get_binding_name(&body.params[idx]).map_or_else(
|
2018-11-27 20:14:15 +00:00
|
|
|
|| Some(vec![]),
|
|
|
|
|name| extract_clone_suggestions(cx, name, replacements, body),
|
|
|
|
)
|
2017-10-08 08:51:44 +00:00
|
|
|
} else {
|
|
|
|
Some(vec![])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-19 18:36:23 +00:00
|
|
|
fn extract_clone_suggestions<'a, 'tcx>(
|
2017-10-08 08:51:44 +00:00
|
|
|
cx: &LateContext<'a, 'tcx>,
|
|
|
|
name: Name,
|
2019-05-17 21:53:54 +00:00
|
|
|
replace: &[(&'static str, &'static str)],
|
2019-12-22 14:42:41 +00:00
|
|
|
body: &'tcx Body<'_>,
|
2017-10-08 08:51:44 +00:00
|
|
|
) -> Option<Vec<(Span, Cow<'static, str>)>> {
|
|
|
|
let mut visitor = PtrCloneVisitor {
|
|
|
|
cx,
|
|
|
|
name,
|
|
|
|
replace,
|
|
|
|
spans: vec![],
|
|
|
|
abort: false,
|
|
|
|
};
|
|
|
|
visitor.visit_body(body);
|
|
|
|
if visitor.abort {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(visitor.spans)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-19 18:36:23 +00:00
|
|
|
struct PtrCloneVisitor<'a, 'tcx> {
|
2017-10-08 08:51:44 +00:00
|
|
|
cx: &'a LateContext<'a, 'tcx>,
|
|
|
|
name: Name,
|
2019-05-17 21:53:54 +00:00
|
|
|
replace: &'a [(&'static str, &'static str)],
|
2017-10-08 08:51:44 +00:00
|
|
|
spans: Vec<(Span, Cow<'static, str>)>,
|
|
|
|
abort: bool,
|
|
|
|
}
|
|
|
|
|
2019-06-19 18:36:23 +00:00
|
|
|
impl<'a, 'tcx> Visitor<'tcx> for PtrCloneVisitor<'a, 'tcx> {
|
2017-10-08 08:51:44 +00:00
|
|
|
fn visit_expr(&mut self, expr: &'tcx Expr) {
|
|
|
|
if self.abort {
|
|
|
|
return;
|
|
|
|
}
|
2019-09-27 15:16:06 +00:00
|
|
|
if let ExprKind::MethodCall(ref seg, _, ref args) = expr.kind {
|
2017-10-08 08:51:44 +00:00
|
|
|
if args.len() == 1 && match_var(&args[0], self.name) {
|
2019-05-17 21:53:54 +00:00
|
|
|
if seg.ident.name.as_str() == "capacity" {
|
2017-10-08 08:51:44 +00:00
|
|
|
self.abort = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for &(fn_name, suffix) in self.replace {
|
2019-05-17 21:53:54 +00:00
|
|
|
if seg.ident.name.as_str() == fn_name {
|
2017-10-08 08:51:44 +00:00
|
|
|
self.spans
|
|
|
|
.push((expr.span, snippet(self.cx, args[0].span, "_") + suffix));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
walk_expr(self, expr);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
|
|
|
|
NestedVisitorMap::None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-28 09:27:06 +00:00
|
|
|
fn get_binding_name(arg: &Param) -> Option<Name> {
|
2017-10-08 08:51:44 +00:00
|
|
|
get_pat_name(&arg.pat)
|
|
|
|
}
|