This commit is contained in:
Mateusz Mikuła
2019-06-02 18:30:40 +02:00
parent f5d6804911
commit 9fefe36737
5 changed files with 21 additions and 31 deletions

View File

@@ -79,7 +79,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for BoxedLocal {
let fn_def_id = cx.tcx.hir().local_def_id_from_hir_id(hir_id); let fn_def_id = cx.tcx.hir().local_def_id_from_hir_id(hir_id);
let region_scope_tree = &cx.tcx.region_scope_tree(fn_def_id); let region_scope_tree = &cx.tcx.region_scope_tree(fn_def_id);
ExprUseVisitor::new(&mut v, cx.tcx, cx.param_env, region_scope_tree, cx.tables, None).consume_body(body); ExprUseVisitor::new(&mut v, cx.tcx, fn_def_id, cx.param_env, region_scope_tree, cx.tables, None).consume_body(body);
for node in v.set { for node in v.set {
span_lint( span_lint(

View File

@@ -1662,7 +1662,7 @@ fn check_for_mutation(
}; };
let def_id = def_id::DefId::local(body.hir_id.owner); let def_id = def_id::DefId::local(body.hir_id.owner);
let region_scope_tree = &cx.tcx.region_scope_tree(def_id); let region_scope_tree = &cx.tcx.region_scope_tree(def_id);
ExprUseVisitor::new(&mut delegate, cx.tcx, cx.param_env, region_scope_tree, cx.tables, None).walk_expr(body); ExprUseVisitor::new(&mut delegate, cx.tcx, def_id, cx.param_env, region_scope_tree, cx.tables, None).walk_expr(body);
delegate.mutation_span() delegate.mutation_span()
} }
@@ -1769,7 +1769,7 @@ impl<'a, 'tcx> VarVisitor<'a, 'tcx> {
} }
let res = self.cx.tables.qpath_res(seqpath, seqexpr.hir_id); let res = self.cx.tables.qpath_res(seqpath, seqexpr.hir_id);
match res { match res {
Res::Local(hir_id) | Res::Upvar(hir_id, ..) => { Res::Local(hir_id) => {
let parent_id = self.cx.tcx.hir().get_parent_item(expr.hir_id); let parent_id = self.cx.tcx.hir().get_parent_item(expr.hir_id);
let parent_def_id = self.cx.tcx.hir().local_def_id_from_hir_id(parent_id); let parent_def_id = self.cx.tcx.hir().local_def_id_from_hir_id(parent_id);
let extent = self.cx.tcx.region_scope_tree(parent_def_id).var_scope(hir_id.local_id); let extent = self.cx.tcx.region_scope_tree(parent_def_id).var_scope(hir_id.local_id);
@@ -1829,24 +1829,13 @@ impl<'a, 'tcx> Visitor<'tcx> for VarVisitor<'a, 'tcx> {
if let QPath::Resolved(None, ref path) = *qpath; if let QPath::Resolved(None, ref path) = *qpath;
if path.segments.len() == 1; if path.segments.len() == 1;
then { then {
match self.cx.tables.qpath_res(qpath, expr.hir_id) { if let Res::Local(local_id) = self.cx.tables.qpath_res(qpath, expr.hir_id) {
Res::Upvar(local_id, ..) => { if local_id == self.var {
if local_id == self.var { self.nonindex = true;
// we are not indexing anything, record that } else {
self.nonindex = true; // not the correct variable, but still a variable
} self.referenced.insert(path.segments[0].ident.name);
} }
Res::Local(local_id) =>
{
if local_id == self.var {
self.nonindex = true;
} else {
// not the correct variable, but still a variable
self.referenced.insert(path.segments[0].ident.name);
}
}
_ => {}
} }
} }
} }
@@ -2378,7 +2367,7 @@ impl<'a, 'tcx> VarCollectorVisitor<'a, 'tcx> {
let res = self.cx.tables.qpath_res(qpath, ex.hir_id); let res = self.cx.tables.qpath_res(qpath, ex.hir_id);
then { then {
match res { match res {
Res::Local(node_id) | Res::Upvar(node_id, ..) => { Res::Local(node_id) => {
self.ids.insert(node_id); self.ids.insert(node_id);
}, },
Res::Def(DefKind::Static, def_id) => { Res::Def(DefKind::Static, def_id) => {

View File

@@ -600,9 +600,10 @@ fn in_attributes_expansion(expr: &Expr) -> bool {
/// Tests whether `res` is a variable defined outside a macro. /// Tests whether `res` is a variable defined outside a macro.
fn non_macro_local(cx: &LateContext<'_, '_>, res: def::Res) -> bool { fn non_macro_local(cx: &LateContext<'_, '_>, res: def::Res) -> bool {
match res { if let def::Res::Local(id) = res {
def::Res::Local(id) | def::Res::Upvar(id, ..) => !in_macro_or_desugar(cx.tcx.hir().span_by_hir_id(id)), !in_macro_or_desugar(cx.tcx.hir().span_by_hir_id(id))
_ => false, } else {
false
} }
} }

View File

@@ -137,7 +137,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NeedlessPassByValue {
} = { } = {
let mut ctx = MovedVariablesCtxt::new(cx); let mut ctx = MovedVariablesCtxt::new(cx);
let region_scope_tree = &cx.tcx.region_scope_tree(fn_def_id); let region_scope_tree = &cx.tcx.region_scope_tree(fn_def_id);
euv::ExprUseVisitor::new(&mut ctx, cx.tcx, cx.param_env, region_scope_tree, cx.tables, None) euv::ExprUseVisitor::new(&mut ctx, cx.tcx, fn_def_id, cx.param_env, region_scope_tree, cx.tables, None)
.consume_body(body); .consume_body(body);
ctx ctx
}; };

View File

@@ -16,7 +16,7 @@ pub fn mutated_variables<'a, 'tcx: 'a>(expr: &'tcx Expr, cx: &'a LateContext<'a,
}; };
let def_id = def_id::DefId::local(expr.hir_id.owner); let def_id = def_id::DefId::local(expr.hir_id.owner);
let region_scope_tree = &cx.tcx.region_scope_tree(def_id); let region_scope_tree = &cx.tcx.region_scope_tree(def_id);
ExprUseVisitor::new(&mut delegate, cx.tcx, cx.param_env, region_scope_tree, cx.tables, None).walk_expr(expr); ExprUseVisitor::new(&mut delegate, cx.tcx, def_id, cx.param_env, region_scope_tree, cx.tables, None).walk_expr(expr);
if delegate.skip { if delegate.skip {
return None; return None;
@@ -29,11 +29,11 @@ pub fn is_potentially_mutated<'a, 'tcx: 'a>(
expr: &'tcx Expr, expr: &'tcx Expr,
cx: &'a LateContext<'a, 'tcx>, cx: &'a LateContext<'a, 'tcx>,
) -> bool { ) -> bool {
let id = match variable.res { if let Res::Local(id) = variable.res {
Res::Local(id) | Res::Upvar(id, ..) => id, mutated_variables(expr, cx).map_or(true, |mutated| mutated.contains(&id))
_ => return true, } else {
}; return true
mutated_variables(expr, cx).map_or(true, |mutated| mutated.contains(&id)) }
} }
struct MutVarsDelegate { struct MutVarsDelegate {