Skip to content

Commit 5f97326

Browse files
committed
Provide diagnostic suggestion in ExprUseVisitor Delegate
The [Delegate trait](https://github.com/rust-lang/rust/blob/981346fc07dd5ef414c5b1b21999f7604cece006/compiler/rustc_typeck/src/expr_use_visitor.rs#L28-L38) currently use `PlaceWithHirId` which is composed of Hir `Place` and the corresponding expression id. Even though this is an accurate way of expressing how a Place is used, it can cause confusion during diagnostics. Eg: ``` let arr : [String; 5]; let [a, ...] = arr; ^^^ E1 ^^^ = ^^E2^^ ``` Here `arr` is moved because of the binding created E1. However, when we point to E1 in diagnostics with the message `arr` was moved, it can be confusing. Rather we would like to report E2 to the user. Closes: rust-lang/project-rfc-2229#20
1 parent 82f775d commit 5f97326

File tree

4 files changed

+16
-16
lines changed

4 files changed

+16
-16
lines changed

clippy_lints/src/escape.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ fn is_argument(map: rustc_middle::hir::map::Map<'_>, id: HirId) -> bool {
116116
}
117117

118118
impl<'a, 'tcx> Delegate<'tcx> for EscapeDelegate<'a, 'tcx> {
119-
fn consume(&mut self, cmt: &PlaceWithHirId<'tcx>, mode: ConsumeMode) {
119+
fn consume(&mut self, cmt: &PlaceWithHirId<'tcx>, _: HirId, mode: ConsumeMode) {
120120
if cmt.place.projections.is_empty() {
121121
if let PlaceBase::Local(lid) = cmt.place.base {
122122
if let ConsumeMode::Move = mode {
@@ -136,15 +136,15 @@ impl<'a, 'tcx> Delegate<'tcx> for EscapeDelegate<'a, 'tcx> {
136136
}
137137
}
138138

139-
fn borrow(&mut self, cmt: &PlaceWithHirId<'tcx>, _: ty::BorrowKind) {
139+
fn borrow(&mut self, cmt: &PlaceWithHirId<'tcx>, _: HirId, _: ty::BorrowKind) {
140140
if cmt.place.projections.is_empty() {
141141
if let PlaceBase::Local(lid) = cmt.place.base {
142142
self.set.remove(&lid);
143143
}
144144
}
145145
}
146146

147-
fn mutate(&mut self, cmt: &PlaceWithHirId<'tcx>) {
147+
fn mutate(&mut self, cmt: &PlaceWithHirId<'tcx>, _: HirId) {
148148
if cmt.place.projections.is_empty() {
149149
let map = &self.cx.tcx.hir();
150150
if is_argument(*map, cmt.hir_id) {

clippy_lints/src/loops.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -1694,28 +1694,28 @@ struct MutatePairDelegate<'a, 'tcx> {
16941694
}
16951695

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

1699-
fn borrow(&mut self, cmt: &PlaceWithHirId<'tcx>, bk: ty::BorrowKind) {
1699+
fn borrow(&mut self, cmt: &PlaceWithHirId<'tcx>, diag_expr_id: HirId, bk: ty::BorrowKind) {
17001700
if let ty::BorrowKind::MutBorrow = bk {
17011701
if let PlaceBase::Local(id) = cmt.place.base {
17021702
if Some(id) == self.hir_id_low {
1703-
self.span_low = Some(self.cx.tcx.hir().span(cmt.hir_id))
1703+
self.span_low = Some(self.cx.tcx.hir().span(diag_expr_id))
17041704
}
17051705
if Some(id) == self.hir_id_high {
1706-
self.span_high = Some(self.cx.tcx.hir().span(cmt.hir_id))
1706+
self.span_high = Some(self.cx.tcx.hir().span(diag_expr_id))
17071707
}
17081708
}
17091709
}
17101710
}
17111711

1712-
fn mutate(&mut self, cmt: &PlaceWithHirId<'tcx>) {
1712+
fn mutate(&mut self, cmt: &PlaceWithHirId<'tcx>, diag_expr_id: HirId) {
17131713
if let PlaceBase::Local(id) = cmt.place.base {
17141714
if Some(id) == self.hir_id_low {
1715-
self.span_low = Some(self.cx.tcx.hir().span(cmt.hir_id))
1715+
self.span_low = Some(self.cx.tcx.hir().span(diag_expr_id))
17161716
}
17171717
if Some(id) == self.hir_id_high {
1718-
self.span_high = Some(self.cx.tcx.hir().span(cmt.hir_id))
1718+
self.span_high = Some(self.cx.tcx.hir().span(diag_expr_id))
17191719
}
17201720
}
17211721
}

clippy_lints/src/needless_pass_by_value.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -325,13 +325,13 @@ impl MovedVariablesCtxt {
325325
}
326326

327327
impl<'tcx> euv::Delegate<'tcx> for MovedVariablesCtxt {
328-
fn consume(&mut self, cmt: &euv::PlaceWithHirId<'tcx>, mode: euv::ConsumeMode) {
328+
fn consume(&mut self, cmt: &euv::PlaceWithHirId<'tcx>, _: HirId, mode: euv::ConsumeMode) {
329329
if let euv::ConsumeMode::Move = mode {
330330
self.move_common(cmt);
331331
}
332332
}
333333

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

336-
fn mutate(&mut self, _: &euv::PlaceWithHirId<'tcx>) {}
336+
fn mutate(&mut self, _: &euv::PlaceWithHirId<'tcx>, _: HirId) {}
337337
}

clippy_lints/src/utils/usage.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,15 @@ impl<'tcx> MutVarsDelegate {
6868
}
6969

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

73-
fn borrow(&mut self, cmt: &PlaceWithHirId<'tcx>, bk: ty::BorrowKind) {
73+
fn borrow(&mut self, cmt: &PlaceWithHirId<'tcx>, _: HirId, bk: ty::BorrowKind) {
7474
if let ty::BorrowKind::MutBorrow = bk {
7575
self.update(&cmt)
7676
}
7777
}
7878

79-
fn mutate(&mut self, cmt: &PlaceWithHirId<'tcx>) {
79+
fn mutate(&mut self, cmt: &PlaceWithHirId<'tcx>, _: HirId) {
8080
self.update(&cmt)
8181
}
8282
}

0 commit comments

Comments
 (0)