From 85bb66480c48d2fea9b54263b7341e3a3bb689d2 Mon Sep 17 00:00:00 2001 From: Manish Goregaokar Date: Thu, 28 Nov 2019 07:09:02 -0800 Subject: [PATCH 1/6] cmt_ -> Place --- clippy_lints/src/escape.rs | 8 ++++---- clippy_lints/src/loops.rs | 8 ++++---- clippy_lints/src/needless_pass_by_value.rs | 10 +++++----- clippy_lints/src/utils/usage.rs | 8 ++++---- 4 files changed, 17 insertions(+), 17 deletions(-) diff --git a/clippy_lints/src/escape.rs b/clippy_lints/src/escape.rs index 58fe4d99c780..91433a8aab70 100644 --- a/clippy_lints/src/escape.rs +++ b/clippy_lints/src/escape.rs @@ -2,7 +2,7 @@ use rustc::hir::intravisit as visit; use rustc::hir::{self, *}; use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass}; use rustc::middle::expr_use_visitor::*; -use rustc::middle::mem_categorization::{cmt_, Categorization}; +use rustc::middle::mem_categorization::{Place, Categorization}; use rustc::ty::layout::LayoutOf; use rustc::ty::{self, Ty}; use rustc::util::nodemap::HirIdSet; @@ -105,7 +105,7 @@ fn is_argument(map: &hir::map::Map<'_>, id: HirId) -> bool { } impl<'a, 'tcx> Delegate<'tcx> for EscapeDelegate<'a, 'tcx> { - fn consume(&mut self, cmt: &cmt_<'tcx>, mode: ConsumeMode) { + fn consume(&mut self, cmt: &Place<'tcx>, mode: ConsumeMode) { if let Categorization::Local(lid) = cmt.cat { if let ConsumeMode::Move = mode { // moved out or in. clearly can't be localized @@ -125,13 +125,13 @@ impl<'a, 'tcx> Delegate<'tcx> for EscapeDelegate<'a, 'tcx> { } } - fn borrow(&mut self, cmt: &cmt_<'tcx>, _: ty::BorrowKind) { + fn borrow(&mut self, cmt: &Place<'tcx>, _: ty::BorrowKind) { if let Categorization::Local(lid) = cmt.cat { self.set.remove(&lid); } } - fn mutate(&mut self, cmt: &cmt_<'tcx>) { + fn mutate(&mut self, cmt: &Place<'tcx>) { let map = &self.cx.tcx.hir(); if is_argument(map, cmt.hir_id) { // Skip closure arguments diff --git a/clippy_lints/src/loops.rs b/clippy_lints/src/loops.rs index 0f0323c13aa1..14ba7fcdef13 100644 --- a/clippy_lints/src/loops.rs +++ b/clippy_lints/src/loops.rs @@ -13,7 +13,7 @@ use crate::consts::{constant, Constant}; use crate::utils::usage::mutated_variables; use crate::utils::{is_type_diagnostic_item, qpath_res, sext, sugg}; use rustc::middle::expr_use_visitor::*; -use rustc::middle::mem_categorization::cmt_; +use rustc::middle::mem_categorization::Place; use rustc::middle::mem_categorization::Categorization; use rustc::ty::subst::Subst; use rustc::ty::{self, Ty}; @@ -1586,9 +1586,9 @@ struct MutatePairDelegate { } impl<'tcx> Delegate<'tcx> for MutatePairDelegate { - fn consume(&mut self, _: &cmt_<'tcx>, _: ConsumeMode) {} + fn consume(&mut self, _: &Place<'tcx>, _: ConsumeMode) {} - fn borrow(&mut self, cmt: &cmt_<'tcx>, bk: ty::BorrowKind) { + fn borrow(&mut self, cmt: &Place<'tcx>, bk: ty::BorrowKind) { if let ty::BorrowKind::MutBorrow = bk { if let Categorization::Local(id) = cmt.cat { if Some(id) == self.hir_id_low { @@ -1601,7 +1601,7 @@ impl<'tcx> Delegate<'tcx> for MutatePairDelegate { } } - fn mutate(&mut self, cmt: &cmt_<'tcx>) { + fn mutate(&mut self, cmt: &Place<'tcx>) { if let Categorization::Local(id) = cmt.cat { if Some(id) == self.hir_id_low { self.span_low = Some(cmt.span) diff --git a/clippy_lints/src/needless_pass_by_value.rs b/clippy_lints/src/needless_pass_by_value.rs index 80f178289e4d..bb5fc6557558 100644 --- a/clippy_lints/src/needless_pass_by_value.rs +++ b/clippy_lints/src/needless_pass_by_value.rs @@ -326,7 +326,7 @@ struct MovedVariablesCtxt { } impl MovedVariablesCtxt { - fn move_common(&mut self, cmt: &mc::cmt_<'_>) { + fn move_common(&mut self, cmt: &mc::Place<'_>) { let cmt = unwrap_downcast_or_interior(cmt); if let mc::Categorization::Local(vid) = cmt.cat { @@ -336,18 +336,18 @@ impl MovedVariablesCtxt { } impl<'tcx> euv::Delegate<'tcx> for MovedVariablesCtxt { - fn consume(&mut self, cmt: &mc::cmt_<'tcx>, mode: euv::ConsumeMode) { + fn consume(&mut self, cmt: &mc::Place<'tcx>, mode: euv::ConsumeMode) { if let euv::ConsumeMode::Move = mode { self.move_common(cmt); } } - fn borrow(&mut self, _: &mc::cmt_<'tcx>, _: ty::BorrowKind) {} + fn borrow(&mut self, _: &mc::Place<'tcx>, _: ty::BorrowKind) {} - fn mutate(&mut self, _: &mc::cmt_<'tcx>) {} + fn mutate(&mut self, _: &mc::Place<'tcx>) {} } -fn unwrap_downcast_or_interior<'a, 'tcx>(mut cmt: &'a mc::cmt_<'tcx>) -> mc::cmt_<'tcx> { +fn unwrap_downcast_or_interior<'a, 'tcx>(mut cmt: &'a mc::Place<'tcx>) -> mc::Place<'tcx> { loop { match cmt.cat { mc::Categorization::Downcast(ref c, _) | mc::Categorization::Interior(ref c, _) => { diff --git a/clippy_lints/src/utils/usage.rs b/clippy_lints/src/utils/usage.rs index 41662099fd3d..48901fc8257a 100644 --- a/clippy_lints/src/utils/usage.rs +++ b/clippy_lints/src/utils/usage.rs @@ -2,7 +2,7 @@ use rustc::hir::def::Res; use rustc::hir::*; use rustc::lint::LateContext; use rustc::middle::expr_use_visitor::*; -use rustc::middle::mem_categorization::cmt_; +use rustc::middle::mem_categorization::Place; use rustc::middle::mem_categorization::Categorization; use rustc::ty; use rustc_data_structures::fx::FxHashSet; @@ -64,15 +64,15 @@ impl<'tcx> MutVarsDelegate { } impl<'tcx> Delegate<'tcx> for MutVarsDelegate { - fn consume(&mut self, _: &cmt_<'tcx>, _: ConsumeMode) {} + fn consume(&mut self, _: &Place<'tcx>, _: ConsumeMode) {} - fn borrow(&mut self, cmt: &cmt_<'tcx>, bk: ty::BorrowKind) { + fn borrow(&mut self, cmt: &Place<'tcx>, bk: ty::BorrowKind) { if let ty::BorrowKind::MutBorrow = bk { self.update(&cmt.cat) } } - fn mutate(&mut self, cmt: &cmt_<'tcx>) { + fn mutate(&mut self, cmt: &Place<'tcx>) { self.update(&cmt.cat) } } From dc9d839410ad28dff5dd857fba9e7e6b66673573 Mon Sep 17 00:00:00 2001 From: Manish Goregaokar Date: Thu, 28 Nov 2019 07:12:05 -0800 Subject: [PATCH 2/6] euv moved from middle to typeck --- clippy_lints/src/escape.rs | 3 +-- clippy_lints/src/loops.rs | 4 +--- clippy_lints/src/needless_pass_by_value.rs | 13 ++++++------- clippy_lints/src/utils/usage.rs | 4 +--- 4 files changed, 9 insertions(+), 15 deletions(-) diff --git a/clippy_lints/src/escape.rs b/clippy_lints/src/escape.rs index 91433a8aab70..0e958c5ad055 100644 --- a/clippy_lints/src/escape.rs +++ b/clippy_lints/src/escape.rs @@ -1,8 +1,7 @@ use rustc::hir::intravisit as visit; use rustc::hir::{self, *}; use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass}; -use rustc::middle::expr_use_visitor::*; -use rustc::middle::mem_categorization::{Place, Categorization}; +use rustc_typeck::expr_use_visitor::*; use rustc::ty::layout::LayoutOf; use rustc::ty::{self, Ty}; use rustc::util::nodemap::HirIdSet; diff --git a/clippy_lints/src/loops.rs b/clippy_lints/src/loops.rs index 14ba7fcdef13..52fa7a2c2e4e 100644 --- a/clippy_lints/src/loops.rs +++ b/clippy_lints/src/loops.rs @@ -12,9 +12,7 @@ use rustc::{declare_lint_pass, declare_tool_lint}; use crate::consts::{constant, Constant}; use crate::utils::usage::mutated_variables; use crate::utils::{is_type_diagnostic_item, qpath_res, sext, sugg}; -use rustc::middle::expr_use_visitor::*; -use rustc::middle::mem_categorization::Place; -use rustc::middle::mem_categorization::Categorization; +use rustc_typeck::expr_use_visitor::*; use rustc::ty::subst::Subst; use rustc::ty::{self, Ty}; use rustc_data_structures::fx::{FxHashMap, FxHashSet}; diff --git a/clippy_lints/src/needless_pass_by_value.rs b/clippy_lints/src/needless_pass_by_value.rs index bb5fc6557558..c9be602fb45f 100644 --- a/clippy_lints/src/needless_pass_by_value.rs +++ b/clippy_lints/src/needless_pass_by_value.rs @@ -8,8 +8,7 @@ use matches::matches; use rustc::hir::intravisit::FnKind; use rustc::hir::*; use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass}; -use rustc::middle::expr_use_visitor as euv; -use rustc::middle::mem_categorization as mc; +use rustc_typeck::expr_use_visitor as euv; use rustc::traits; use rustc::ty::{self, RegionKind, TypeFoldable}; use rustc::{declare_lint_pass, declare_tool_lint}; @@ -326,7 +325,7 @@ struct MovedVariablesCtxt { } impl MovedVariablesCtxt { - fn move_common(&mut self, cmt: &mc::Place<'_>) { + fn move_common(&mut self, cmt: &euv::Place<'_>) { let cmt = unwrap_downcast_or_interior(cmt); if let mc::Categorization::Local(vid) = cmt.cat { @@ -336,18 +335,18 @@ impl MovedVariablesCtxt { } impl<'tcx> euv::Delegate<'tcx> for MovedVariablesCtxt { - fn consume(&mut self, cmt: &mc::Place<'tcx>, mode: euv::ConsumeMode) { + fn consume(&mut self, cmt: &euv::Place<'tcx>, mode: euv::ConsumeMode) { if let euv::ConsumeMode::Move = mode { self.move_common(cmt); } } - fn borrow(&mut self, _: &mc::Place<'tcx>, _: ty::BorrowKind) {} + fn borrow(&mut self, _: &euv::Place<'tcx>, _: ty::BorrowKind) {} - fn mutate(&mut self, _: &mc::Place<'tcx>) {} + fn mutate(&mut self, _: &euv::Place<'tcx>) {} } -fn unwrap_downcast_or_interior<'a, 'tcx>(mut cmt: &'a mc::Place<'tcx>) -> mc::Place<'tcx> { +fn unwrap_downcast_or_interior<'a, 'tcx>(mut cmt: &'a euv::Place<'tcx>) -> euv::Place<'tcx> { loop { match cmt.cat { mc::Categorization::Downcast(ref c, _) | mc::Categorization::Interior(ref c, _) => { diff --git a/clippy_lints/src/utils/usage.rs b/clippy_lints/src/utils/usage.rs index 48901fc8257a..8cd5d7d2d02a 100644 --- a/clippy_lints/src/utils/usage.rs +++ b/clippy_lints/src/utils/usage.rs @@ -1,9 +1,7 @@ use rustc::hir::def::Res; use rustc::hir::*; use rustc::lint::LateContext; -use rustc::middle::expr_use_visitor::*; -use rustc::middle::mem_categorization::Place; -use rustc::middle::mem_categorization::Categorization; +use rustc_typeck::expr_use_visitor::*; use rustc::ty; use rustc_data_structures::fx::FxHashSet; From c1c5c3522da22385a30d09843c7eed87ce68c1aa Mon Sep 17 00:00:00 2001 From: Manish Goregaokar Date: Thu, 28 Nov 2019 07:20:37 -0800 Subject: [PATCH 3/6] Fix arguments on ExprUseVisitor::new --- clippy_lints/src/escape.rs | 3 +-- clippy_lints/src/loops.rs | 2 -- clippy_lints/src/needless_pass_by_value.rs | 3 +-- clippy_lints/src/utils/usage.rs | 2 -- 4 files changed, 2 insertions(+), 8 deletions(-) diff --git a/clippy_lints/src/escape.rs b/clippy_lints/src/escape.rs index 0e958c5ad055..a3cbac820c6b 100644 --- a/clippy_lints/src/escape.rs +++ b/clippy_lints/src/escape.rs @@ -76,8 +76,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for BoxedLocal { }; let fn_def_id = cx.tcx.hir().local_def_id(hir_id); - let region_scope_tree = &cx.tcx.region_scope_tree(fn_def_id); - ExprUseVisitor::new(&mut v, cx.tcx, fn_def_id, cx.param_env, region_scope_tree, cx.tables).consume_body(body); + ExprUseVisitor::new(&mut v, cx.tcx, fn_def_id, cx.param_env, cx.tables).consume_body(body); for node in v.set { span_lint( diff --git a/clippy_lints/src/loops.rs b/clippy_lints/src/loops.rs index 52fa7a2c2e4e..3b86fddd8bd9 100644 --- a/clippy_lints/src/loops.rs +++ b/clippy_lints/src/loops.rs @@ -1678,13 +1678,11 @@ fn check_for_mutation( span_high: None, }; let def_id = def_id::DefId::local(body.hir_id.owner); - let region_scope_tree = &cx.tcx.region_scope_tree(def_id); ExprUseVisitor::new( &mut delegate, cx.tcx, def_id, cx.param_env, - region_scope_tree, cx.tables, ) .walk_expr(body); diff --git a/clippy_lints/src/needless_pass_by_value.rs b/clippy_lints/src/needless_pass_by_value.rs index c9be602fb45f..64786f48719a 100644 --- a/clippy_lints/src/needless_pass_by_value.rs +++ b/clippy_lints/src/needless_pass_by_value.rs @@ -134,8 +134,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NeedlessPassByValue { .. } = { let mut ctx = MovedVariablesCtxt::default(); - let region_scope_tree = &cx.tcx.region_scope_tree(fn_def_id); - euv::ExprUseVisitor::new(&mut ctx, cx.tcx, fn_def_id, cx.param_env, region_scope_tree, cx.tables) + euv::ExprUseVisitor::new(&mut ctx, cx.tcx, fn_def_id, cx.param_env, cx.tables) .consume_body(body); ctx }; diff --git a/clippy_lints/src/utils/usage.rs b/clippy_lints/src/utils/usage.rs index 8cd5d7d2d02a..e71dfd7401fc 100644 --- a/clippy_lints/src/utils/usage.rs +++ b/clippy_lints/src/utils/usage.rs @@ -12,13 +12,11 @@ pub fn mutated_variables<'a, 'tcx>(expr: &'tcx Expr, cx: &'a LateContext<'a, 'tc skip: false, }; let def_id = def_id::DefId::local(expr.hir_id.owner); - let region_scope_tree = &cx.tcx.region_scope_tree(def_id); ExprUseVisitor::new( &mut delegate, cx.tcx, def_id, cx.param_env, - region_scope_tree, cx.tables, ) .walk_expr(expr); From 45842e5131b763102abb20a6ba845d5acafdf8a5 Mon Sep 17 00:00:00 2001 From: Manish Goregaokar Date: Thu, 28 Nov 2019 07:33:12 -0800 Subject: [PATCH 4/6] Fix categorizations --- clippy_lints/src/escape.rs | 6 +++--- clippy_lints/src/loops.rs | 4 ++-- clippy_lints/src/needless_pass_by_value.rs | 14 +------------- clippy_lints/src/utils/usage.rs | 13 ++++++------- 4 files changed, 12 insertions(+), 25 deletions(-) diff --git a/clippy_lints/src/escape.rs b/clippy_lints/src/escape.rs index a3cbac820c6b..62fdf295aa79 100644 --- a/clippy_lints/src/escape.rs +++ b/clippy_lints/src/escape.rs @@ -104,14 +104,14 @@ fn is_argument(map: &hir::map::Map<'_>, id: HirId) -> bool { impl<'a, 'tcx> Delegate<'tcx> for EscapeDelegate<'a, 'tcx> { fn consume(&mut self, cmt: &Place<'tcx>, mode: ConsumeMode) { - if let Categorization::Local(lid) = cmt.cat { + if let PlaceBase::Local(lid) = cmt.base { if let ConsumeMode::Move = mode { // moved out or in. clearly can't be localized self.set.remove(&lid); } } let map = &self.cx.tcx.hir(); - if let Categorization::Local(lid) = cmt.cat { + if let PlaceBase::Local(lid) = cmt.base { if let Some(Node::Binding(_)) = map.find(cmt.hir_id) { if self.set.contains(&lid) { // let y = x where x is known @@ -124,7 +124,7 @@ impl<'a, 'tcx> Delegate<'tcx> for EscapeDelegate<'a, 'tcx> { } fn borrow(&mut self, cmt: &Place<'tcx>, _: ty::BorrowKind) { - if let Categorization::Local(lid) = cmt.cat { + if let PlaceBase::Local(lid) = cmt.base { self.set.remove(&lid); } } diff --git a/clippy_lints/src/loops.rs b/clippy_lints/src/loops.rs index 3b86fddd8bd9..948367d8cee7 100644 --- a/clippy_lints/src/loops.rs +++ b/clippy_lints/src/loops.rs @@ -1588,7 +1588,7 @@ impl<'tcx> Delegate<'tcx> for MutatePairDelegate { fn borrow(&mut self, cmt: &Place<'tcx>, bk: ty::BorrowKind) { if let ty::BorrowKind::MutBorrow = bk { - if let Categorization::Local(id) = cmt.cat { + if let PlaceBase::Local(id) = cmt.base { if Some(id) == self.hir_id_low { self.span_low = Some(cmt.span) } @@ -1600,7 +1600,7 @@ impl<'tcx> Delegate<'tcx> for MutatePairDelegate { } fn mutate(&mut self, cmt: &Place<'tcx>) { - if let Categorization::Local(id) = cmt.cat { + if let PlaceBase::Local(id) = cmt.base { if Some(id) == self.hir_id_low { self.span_low = Some(cmt.span) } diff --git a/clippy_lints/src/needless_pass_by_value.rs b/clippy_lints/src/needless_pass_by_value.rs index 64786f48719a..617841f2e5de 100644 --- a/clippy_lints/src/needless_pass_by_value.rs +++ b/clippy_lints/src/needless_pass_by_value.rs @@ -325,9 +325,7 @@ struct MovedVariablesCtxt { impl MovedVariablesCtxt { fn move_common(&mut self, cmt: &euv::Place<'_>) { - let cmt = unwrap_downcast_or_interior(cmt); - - if let mc::Categorization::Local(vid) = cmt.cat { + if let euv::PlaceBase::Local(vid) = cmt.base { self.moved_vars.insert(vid); } } @@ -345,13 +343,3 @@ impl<'tcx> euv::Delegate<'tcx> for MovedVariablesCtxt { fn mutate(&mut self, _: &euv::Place<'tcx>) {} } -fn unwrap_downcast_or_interior<'a, 'tcx>(mut cmt: &'a euv::Place<'tcx>) -> euv::Place<'tcx> { - loop { - match cmt.cat { - mc::Categorization::Downcast(ref c, _) | mc::Categorization::Interior(ref c, _) => { - cmt = c; - }, - _ => return (*cmt).clone(), - } - } -} diff --git a/clippy_lints/src/utils/usage.rs b/clippy_lints/src/utils/usage.rs index e71dfd7401fc..59220e732a02 100644 --- a/clippy_lints/src/utils/usage.rs +++ b/clippy_lints/src/utils/usage.rs @@ -42,18 +42,17 @@ struct MutVarsDelegate { impl<'tcx> MutVarsDelegate { #[allow(clippy::similar_names)] - fn update(&mut self, cat: &'tcx Categorization<'_>) { - match *cat { - Categorization::Local(id) => { + fn update(&mut self, cat: &Place<'tcx>) { + match cat.base { + PlaceBase::Local(id) => { self.used_mutably.insert(id); }, - Categorization::Upvar(_) => { + PlaceBase::Upvar(_) => { //FIXME: This causes false negatives. We can't get the `NodeId` from //`Categorization::Upvar(_)`. So we search for any `Upvar`s in the //`while`-body, not just the ones in the condition. self.skip = true }, - Categorization::Deref(ref cmt, _) | Categorization::Interior(ref cmt, _) => self.update(&cmt.cat), _ => {}, } } @@ -64,11 +63,11 @@ impl<'tcx> Delegate<'tcx> for MutVarsDelegate { fn borrow(&mut self, cmt: &Place<'tcx>, bk: ty::BorrowKind) { if let ty::BorrowKind::MutBorrow = bk { - self.update(&cmt.cat) + self.update(&cmt) } } fn mutate(&mut self, cmt: &Place<'tcx>) { - self.update(&cmt.cat) + self.update(&cmt) } } From b2523afae4fd1896844e960db2e192206f71e903 Mon Sep 17 00:00:00 2001 From: flip1995 Date: Fri, 29 Nov 2019 11:12:19 +0100 Subject: [PATCH 5/6] Use infer_ctxt --- clippy_lints/src/escape.rs | 6 ++++-- clippy_lints/src/loops.rs | 13 ++++--------- clippy_lints/src/needless_pass_by_value.rs | 8 ++++---- clippy_lints/src/utils/usage.rs | 13 ++++--------- 4 files changed, 16 insertions(+), 24 deletions(-) diff --git a/clippy_lints/src/escape.rs b/clippy_lints/src/escape.rs index 62fdf295aa79..de9e5cd4e869 100644 --- a/clippy_lints/src/escape.rs +++ b/clippy_lints/src/escape.rs @@ -1,11 +1,11 @@ use rustc::hir::intravisit as visit; use rustc::hir::{self, *}; use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass}; -use rustc_typeck::expr_use_visitor::*; use rustc::ty::layout::LayoutOf; use rustc::ty::{self, Ty}; use rustc::util::nodemap::HirIdSet; use rustc::{declare_tool_lint, impl_lint_pass}; +use rustc_typeck::expr_use_visitor::*; use syntax::source_map::Span; use crate::utils::span_lint; @@ -76,7 +76,9 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for BoxedLocal { }; let fn_def_id = cx.tcx.hir().local_def_id(hir_id); - ExprUseVisitor::new(&mut v, cx.tcx, fn_def_id, cx.param_env, cx.tables).consume_body(body); + cx.tcx.infer_ctxt().enter(|infcx| { + ExprUseVisitor::new(&mut v, &infcx, fn_def_id, cx.param_env, cx.tables).consume_body(body); + }); for node in v.set { span_lint( diff --git a/clippy_lints/src/loops.rs b/clippy_lints/src/loops.rs index 948367d8cee7..4212f9c1e64f 100644 --- a/clippy_lints/src/loops.rs +++ b/clippy_lints/src/loops.rs @@ -12,11 +12,11 @@ use rustc::{declare_lint_pass, declare_tool_lint}; use crate::consts::{constant, Constant}; use crate::utils::usage::mutated_variables; use crate::utils::{is_type_diagnostic_item, qpath_res, sext, sugg}; -use rustc_typeck::expr_use_visitor::*; use rustc::ty::subst::Subst; use rustc::ty::{self, Ty}; use rustc_data_structures::fx::{FxHashMap, FxHashSet}; use rustc_errors::Applicability; +use rustc_typeck::expr_use_visitor::*; use std::iter::{once, Iterator}; use std::mem; use syntax::ast; @@ -1678,14 +1678,9 @@ fn check_for_mutation( span_high: None, }; let def_id = def_id::DefId::local(body.hir_id.owner); - ExprUseVisitor::new( - &mut delegate, - cx.tcx, - def_id, - cx.param_env, - cx.tables, - ) - .walk_expr(body); + cx.tcx.infer_ctxt().enter(|infcx| { + ExprUseVisitor::new(&mut delegate, &infcx, def_id, cx.param_env, cx.tables).walk_expr(body); + }); delegate.mutation_span() } diff --git a/clippy_lints/src/needless_pass_by_value.rs b/clippy_lints/src/needless_pass_by_value.rs index 617841f2e5de..05894de5cb70 100644 --- a/clippy_lints/src/needless_pass_by_value.rs +++ b/clippy_lints/src/needless_pass_by_value.rs @@ -8,13 +8,13 @@ use matches::matches; use rustc::hir::intravisit::FnKind; use rustc::hir::*; use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass}; -use rustc_typeck::expr_use_visitor as euv; use rustc::traits; use rustc::ty::{self, RegionKind, TypeFoldable}; use rustc::{declare_lint_pass, declare_tool_lint}; use rustc_data_structures::fx::{FxHashMap, FxHashSet}; use rustc_errors::Applicability; use rustc_target::spec::abi::Abi; +use rustc_typeck::expr_use_visitor as euv; use std::borrow::Cow; use syntax::ast::Attribute; use syntax::errors::DiagnosticBuilder; @@ -134,8 +134,9 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NeedlessPassByValue { .. } = { let mut ctx = MovedVariablesCtxt::default(); - euv::ExprUseVisitor::new(&mut ctx, cx.tcx, fn_def_id, cx.param_env, cx.tables) - .consume_body(body); + cx.tcx.infer_ctxt().enter(|infcx| { + euv::ExprUseVisitor::new(&mut ctx, &infcx, fn_def_id, cx.param_env, cx.tables).consume_body(body); + }); ctx }; @@ -342,4 +343,3 @@ impl<'tcx> euv::Delegate<'tcx> for MovedVariablesCtxt { fn mutate(&mut self, _: &euv::Place<'tcx>) {} } - diff --git a/clippy_lints/src/utils/usage.rs b/clippy_lints/src/utils/usage.rs index 59220e732a02..089ebafbb7fd 100644 --- a/clippy_lints/src/utils/usage.rs +++ b/clippy_lints/src/utils/usage.rs @@ -1,9 +1,9 @@ use rustc::hir::def::Res; use rustc::hir::*; use rustc::lint::LateContext; -use rustc_typeck::expr_use_visitor::*; use rustc::ty; use rustc_data_structures::fx::FxHashSet; +use rustc_typeck::expr_use_visitor::*; /// Returns a set of mutated local variable IDs, or `None` if mutations could not be determined. pub fn mutated_variables<'a, 'tcx>(expr: &'tcx Expr, cx: &'a LateContext<'a, 'tcx>) -> Option> { @@ -12,14 +12,9 @@ pub fn mutated_variables<'a, 'tcx>(expr: &'tcx Expr, cx: &'a LateContext<'a, 'tc skip: false, }; let def_id = def_id::DefId::local(expr.hir_id.owner); - ExprUseVisitor::new( - &mut delegate, - cx.tcx, - def_id, - cx.param_env, - cx.tables, - ) - .walk_expr(expr); + cx.tcx.infer_ctxt().enter(|infcx| { + ExprUseVisitor::new(&mut delegate, &infcx, def_id, cx.param_env, cx.tables).walk_expr(expr); + }); if delegate.skip { return None; From 604e6ba0e2fb011987554f878bed4b8ba9bc00ec Mon Sep 17 00:00:00 2001 From: flip1995 Date: Fri, 29 Nov 2019 12:57:10 +0100 Subject: [PATCH 6/6] Add projections check to EUV for escape analysis --- clippy_lints/src/escape.rs | 54 ++++++++++++++++++++------------------ 1 file changed, 29 insertions(+), 25 deletions(-) diff --git a/clippy_lints/src/escape.rs b/clippy_lints/src/escape.rs index de9e5cd4e869..e796b465944d 100644 --- a/clippy_lints/src/escape.rs +++ b/clippy_lints/src/escape.rs @@ -106,44 +106,48 @@ fn is_argument(map: &hir::map::Map<'_>, id: HirId) -> bool { impl<'a, 'tcx> Delegate<'tcx> for EscapeDelegate<'a, 'tcx> { fn consume(&mut self, cmt: &Place<'tcx>, mode: ConsumeMode) { - if let PlaceBase::Local(lid) = cmt.base { - if let ConsumeMode::Move = mode { - // moved out or in. clearly can't be localized - self.set.remove(&lid); - } - } - let map = &self.cx.tcx.hir(); - if let PlaceBase::Local(lid) = cmt.base { - if let Some(Node::Binding(_)) = map.find(cmt.hir_id) { - if self.set.contains(&lid) { - // let y = x where x is known - // remove x, insert y - self.set.insert(cmt.hir_id); + if cmt.projections.is_empty() { + if let PlaceBase::Local(lid) = cmt.base { + if let ConsumeMode::Move = mode { + // moved out or in. clearly can't be localized self.set.remove(&lid); } + let map = &self.cx.tcx.hir(); + if let Some(Node::Binding(_)) = map.find(cmt.hir_id) { + if self.set.contains(&lid) { + // let y = x where x is known + // remove x, insert y + self.set.insert(cmt.hir_id); + self.set.remove(&lid); + } + } } } } fn borrow(&mut self, cmt: &Place<'tcx>, _: ty::BorrowKind) { - if let PlaceBase::Local(lid) = cmt.base { - self.set.remove(&lid); + if cmt.projections.is_empty() { + if let PlaceBase::Local(lid) = cmt.base { + self.set.remove(&lid); + } } } fn mutate(&mut self, cmt: &Place<'tcx>) { - let map = &self.cx.tcx.hir(); - if is_argument(map, cmt.hir_id) { - // Skip closure arguments - let parent_id = map.get_parent_node(cmt.hir_id); - if let Some(Node::Expr(..)) = map.find(map.get_parent_node(parent_id)) { - return; - } + if cmt.projections.is_empty() { + let map = &self.cx.tcx.hir(); + if is_argument(map, cmt.hir_id) { + // Skip closure arguments + let parent_id = map.get_parent_node(cmt.hir_id); + if let Some(Node::Expr(..)) = map.find(map.get_parent_node(parent_id)) { + return; + } - if is_non_trait_box(cmt.ty) && !self.is_large_box(cmt.ty) { - self.set.insert(cmt.hir_id); + if is_non_trait_box(cmt.ty) && !self.is_large_box(cmt.ty) { + self.set.insert(cmt.hir_id); + } + return; } - return; } } }