Skip to content

Commit 0c1531a

Browse files
committed
Auto merge of rust-lang#78662 - sexxi-goose:add_expr_id_to_delegate, r=nikomatsakis
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 r? `@ghost`
2 parents 83edb2f + 5f97326 commit 0c1531a

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
@@ -115,7 +115,7 @@ fn is_argument(map: rustc_middle::hir::map::Map<'_>, id: HirId) -> bool {
115115
}
116116

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

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

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

clippy_lints/src/loops.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -1957,28 +1957,28 @@ struct MutatePairDelegate<'a, 'tcx> {
19571957
}
19581958

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

1962-
fn borrow(&mut self, cmt: &PlaceWithHirId<'tcx>, bk: ty::BorrowKind) {
1962+
fn borrow(&mut self, cmt: &PlaceWithHirId<'tcx>, diag_expr_id: HirId, bk: ty::BorrowKind) {
19631963
if let ty::BorrowKind::MutBorrow = bk {
19641964
if let PlaceBase::Local(id) = cmt.place.base {
19651965
if Some(id) == self.hir_id_low {
1966-
self.span_low = Some(self.cx.tcx.hir().span(cmt.hir_id))
1966+
self.span_low = Some(self.cx.tcx.hir().span(diag_expr_id))
19671967
}
19681968
if Some(id) == self.hir_id_high {
1969-
self.span_high = Some(self.cx.tcx.hir().span(cmt.hir_id))
1969+
self.span_high = Some(self.cx.tcx.hir().span(diag_expr_id))
19701970
}
19711971
}
19721972
}
19731973
}
19741974

1975-
fn mutate(&mut self, cmt: &PlaceWithHirId<'tcx>) {
1975+
fn mutate(&mut self, cmt: &PlaceWithHirId<'tcx>, diag_expr_id: HirId) {
19761976
if let PlaceBase::Local(id) = cmt.place.base {
19771977
if Some(id) == self.hir_id_low {
1978-
self.span_low = Some(self.cx.tcx.hir().span(cmt.hir_id))
1978+
self.span_low = Some(self.cx.tcx.hir().span(diag_expr_id))
19791979
}
19801980
if Some(id) == self.hir_id_high {
1981-
self.span_high = Some(self.cx.tcx.hir().span(cmt.hir_id))
1981+
self.span_high = Some(self.cx.tcx.hir().span(diag_expr_id))
19821982
}
19831983
}
19841984
}

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)