Skip to content

Commit 6c3774e

Browse files
committed
ExprUseVisitor::Delegate consume only when moving
1 parent 36f51c9 commit 6c3774e

File tree

6 files changed

+21
-51
lines changed

6 files changed

+21
-51
lines changed

compiler/rustc_typeck/src/check/upvar.rs

+5-21
Original file line numberDiff line numberDiff line change
@@ -1528,19 +1528,11 @@ impl<'a, 'tcx> InferBorrowKind<'a, 'tcx> {
15281528
&mut self,
15291529
place_with_id: &PlaceWithHirId<'tcx>,
15301530
diag_expr_id: hir::HirId,
1531-
mode: euv::ConsumeMode,
15321531
) {
15331532
debug!(
1534-
"adjust_upvar_borrow_kind_for_consume(place_with_id={:?}, diag_expr_id={:?}, mode={:?})",
1535-
place_with_id, diag_expr_id, mode
1533+
"adjust_upvar_borrow_kind_for_consume(place_with_id={:?}, diag_expr_id={:?})",
1534+
place_with_id, diag_expr_id
15361535
);
1537-
1538-
// Copy types in ByValue scenarios need should be treated as ImmBorrows
1539-
match mode {
1540-
euv::ConsumeMode::Copy => unreachable!(),
1541-
euv::ConsumeMode::Move => {}
1542-
};
1543-
15441536
let tcx = self.fcx.tcx;
15451537
let upvar_id = if let PlaceBase::Upvar(upvar_id) = place_with_id.place.base {
15461538
upvar_id
@@ -1715,22 +1707,14 @@ impl<'a, 'tcx> euv::Delegate<'tcx> for InferBorrowKind<'a, 'tcx> {
17151707
}
17161708
}
17171709

1718-
fn consume(
1719-
&mut self,
1720-
place_with_id: &PlaceWithHirId<'tcx>,
1721-
diag_expr_id: hir::HirId,
1722-
mode: euv::ConsumeMode,
1723-
) {
1724-
debug!(
1725-
"consume(place_with_id={:?}, diag_expr_id={:?}, mode={:?})",
1726-
place_with_id, diag_expr_id, mode
1727-
);
1710+
fn consume(&mut self, place_with_id: &PlaceWithHirId<'tcx>, diag_expr_id: hir::HirId) {
1711+
debug!("consume(place_with_id={:?}, diag_expr_id={:?})", place_with_id, diag_expr_id);
17281712

17291713
if !self.capture_information.contains_key(&place_with_id.place) {
17301714
self.init_capture_info_for_place(&place_with_id, diag_expr_id);
17311715
}
17321716

1733-
self.adjust_upvar_borrow_kind_for_consume(&place_with_id, diag_expr_id, mode);
1717+
self.adjust_upvar_borrow_kind_for_consume(&place_with_id, diag_expr_id);
17341718
}
17351719

17361720
fn borrow(

compiler/rustc_typeck/src/expr_use_visitor.rs

+7-16
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
//! normal visitor, which just walks the entire body in one shot, the
33
//! `ExprUseVisitor` determines how expressions are being used.
44
5-
pub use self::ConsumeMode::*;
6-
75
// Export these here so that Clippy can use them.
86
pub use rustc_middle::hir::place::{Place, PlaceBase, PlaceWithHirId, Projection};
97

@@ -28,19 +26,14 @@ use crate::mem_categorization as mc;
2826
/// This trait defines the callbacks you can expect to receive when
2927
/// employing the ExprUseVisitor.
3028
pub trait Delegate<'tcx> {
31-
// The value found at `place` is either copied or moved, depending
29+
// The value found at `place` is moved, depending
3230
// on `mode`. Where `diag_expr_id` is the id used for diagnostics for `place`.
3331
//
3432
// The parameter `diag_expr_id` indicates the HIR id that ought to be used for
3533
// diagnostics. Around pattern matching such as `let pat = expr`, the diagnostic
3634
// id will be the id of the expression `expr` but the place itself will have
3735
// the id of the binding in the pattern `pat`.
38-
fn consume(
39-
&mut self,
40-
place_with_id: &PlaceWithHirId<'tcx>,
41-
diag_expr_id: hir::HirId,
42-
mode: ConsumeMode,
43-
);
36+
fn consume(&mut self, place_with_id: &PlaceWithHirId<'tcx>, diag_expr_id: hir::HirId);
4437

4538
// The value found at `place` is being borrowed with kind `bk`.
4639
// `diag_expr_id` is the id used for diagnostics (see `consume` for more details).
@@ -60,7 +53,7 @@ pub trait Delegate<'tcx> {
6053
}
6154

6255
#[derive(Copy, Clone, PartialEq, Debug)]
63-
pub enum ConsumeMode {
56+
enum ConsumeMode {
6457
Copy, // reference to x where x has a type that copies
6558
Move, // reference to x where x has a type that moves
6659
}
@@ -146,7 +139,7 @@ impl<'a, 'tcx> ExprUseVisitor<'a, 'tcx> {
146139
let mode = copy_or_move(&self.mc, place_with_id);
147140

148141
match mode {
149-
ConsumeMode::Move => self.delegate.consume(place_with_id, diag_expr_id, mode),
142+
ConsumeMode::Move => self.delegate.consume(place_with_id, diag_expr_id),
150143
ConsumeMode::Copy => {
151144
self.delegate.borrow(place_with_id, diag_expr_id, ty::BorrowKind::ImmBorrow)
152145
}
@@ -662,9 +655,7 @@ impl<'a, 'tcx> ExprUseVisitor<'a, 'tcx> {
662655
debug!("walk_pat binding consuming pat");
663656
let mode = copy_or_move(mc, &place);
664657
match mode {
665-
ConsumeMode::Move => {
666-
delegate.consume(place, discr_place.hir_id, mode)
667-
}
658+
ConsumeMode::Move => delegate.consume(place, discr_place.hir_id),
668659
ConsumeMode::Copy => delegate.borrow(
669660
place,
670661
discr_place.hir_id,
@@ -812,8 +803,8 @@ fn copy_or_move<'a, 'tcx>(
812803
place_with_id.place.ty(),
813804
mc.tcx().hir().span(place_with_id.hir_id),
814805
) {
815-
Move
806+
ConsumeMode::Move
816807
} else {
817-
Copy
808+
ConsumeMode::Copy
818809
}
819810
}

src/tools/clippy/clippy_lints/src/escape.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use rustc_span::source_map::Span;
1111
use rustc_span::symbol::kw;
1212
use rustc_target::abi::LayoutOf;
1313
use rustc_target::spec::abi::Abi;
14-
use rustc_typeck::expr_use_visitor::{ConsumeMode, Delegate, ExprUseVisitor, PlaceBase, PlaceWithHirId};
14+
use rustc_typeck::expr_use_visitor::{Delegate, ExprUseVisitor, PlaceBase, PlaceWithHirId};
1515

1616
#[derive(Copy, Clone)]
1717
pub struct BoxedLocal {
@@ -133,13 +133,10 @@ fn is_argument(map: rustc_middle::hir::map::Map<'_>, id: HirId) -> bool {
133133
}
134134

135135
impl<'a, 'tcx> Delegate<'tcx> for EscapeDelegate<'a, 'tcx> {
136-
fn consume(&mut self, cmt: &PlaceWithHirId<'tcx>, _: HirId, mode: ConsumeMode) {
136+
fn consume(&mut self, cmt: &PlaceWithHirId<'tcx>, _: HirId) {
137137
if cmt.place.projections.is_empty() {
138138
if let PlaceBase::Local(lid) = cmt.place.base {
139-
if let ConsumeMode::Move = mode {
140-
// moved out or in. clearly can't be localized
141-
self.set.remove(&lid);
142-
}
139+
self.set.remove(&lid);
143140
let map = &self.cx.tcx.hir();
144141
if let Some(Node::Binding(_)) = map.find(cmt.hir_id) {
145142
if self.set.contains(&lid) {

src/tools/clippy/clippy_lints/src/loops/mut_range_bound.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rustc_infer::infer::TyCtxtInferExt;
77
use rustc_lint::LateContext;
88
use rustc_middle::{mir::FakeReadCause, ty};
99
use rustc_span::source_map::Span;
10-
use rustc_typeck::expr_use_visitor::{ConsumeMode, Delegate, ExprUseVisitor, PlaceBase, PlaceWithHirId};
10+
use rustc_typeck::expr_use_visitor::{Delegate, ExprUseVisitor, PlaceBase, PlaceWithHirId};
1111

1212
pub(super) fn check(cx: &LateContext<'_>, arg: &Expr<'_>, body: &Expr<'_>) {
1313
if let Some(higher::Range {
@@ -82,7 +82,7 @@ struct MutatePairDelegate<'a, 'tcx> {
8282
}
8383

8484
impl<'tcx> Delegate<'tcx> for MutatePairDelegate<'_, 'tcx> {
85-
fn consume(&mut self, _: &PlaceWithHirId<'tcx>, _: HirId, _: ConsumeMode) {}
85+
fn consume(&mut self, _: &PlaceWithHirId<'tcx>, _: HirId) {}
8686

8787
fn borrow(&mut self, cmt: &PlaceWithHirId<'tcx>, diag_expr_id: HirId, bk: ty::BorrowKind) {
8888
if let ty::BorrowKind::MutBorrow = bk {

src/tools/clippy/clippy_lints/src/needless_pass_by_value.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -326,10 +326,8 @@ impl MovedVariablesCtxt {
326326
}
327327

328328
impl<'tcx> euv::Delegate<'tcx> for MovedVariablesCtxt {
329-
fn consume(&mut self, cmt: &euv::PlaceWithHirId<'tcx>, _: HirId, mode: euv::ConsumeMode) {
330-
if let euv::ConsumeMode::Move = mode {
331-
self.move_common(cmt);
332-
}
329+
fn consume(&mut self, cmt: &euv::PlaceWithHirId<'tcx>, _: HirId) {
330+
self.move_common(cmt);
333331
}
334332

335333
fn borrow(&mut self, _: &euv::PlaceWithHirId<'tcx>, _: HirId, _: ty::BorrowKind) {}

src/tools/clippy/clippy_utils/src/usage.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use rustc_lint::LateContext;
1010
use rustc_middle::hir::map::Map;
1111
use rustc_middle::mir::FakeReadCause;
1212
use rustc_middle::ty;
13-
use rustc_typeck::expr_use_visitor::{ConsumeMode, Delegate, ExprUseVisitor, PlaceBase, PlaceWithHirId};
13+
use rustc_typeck::expr_use_visitor::{Delegate, ExprUseVisitor, PlaceBase, PlaceWithHirId};
1414

1515
/// Returns a set of mutated local variable IDs, or `None` if mutations could not be determined.
1616
pub fn mutated_variables<'tcx>(expr: &'tcx Expr<'_>, cx: &LateContext<'tcx>) -> Option<HirIdSet> {
@@ -67,7 +67,7 @@ impl<'tcx> MutVarsDelegate {
6767
}
6868

6969
impl<'tcx> Delegate<'tcx> for MutVarsDelegate {
70-
fn consume(&mut self, _: &PlaceWithHirId<'tcx>, _: HirId, _: ConsumeMode) {}
70+
fn consume(&mut self, _: &PlaceWithHirId<'tcx>, _: HirId) {}
7171

7272
fn borrow(&mut self, cmt: &PlaceWithHirId<'tcx>, _: HirId, bk: ty::BorrowKind) {
7373
if let ty::BorrowKind::MutBorrow = bk {

0 commit comments

Comments
 (0)