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