Skip to content

Commit 69c71da

Browse files
author
Lukas Markeffsky
committed
fix false negative for unused_mut
1 parent 43a7802 commit 69c71da

File tree

3 files changed

+23
-6
lines changed

3 files changed

+23
-6
lines changed

compiler/rustc_borrowck/src/lib.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -935,6 +935,7 @@ enum InitializationRequiringAction {
935935
PartialAssignment,
936936
}
937937

938+
#[derive(Debug)]
938939
struct RootPlace<'tcx> {
939940
place_local: Local,
940941
place_projection: &'tcx [PlaceElem<'tcx>],
@@ -1848,11 +1849,6 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
18481849
// is allowed, remove this match arm.
18491850
ty::Adt(..) | ty::Tuple(..) => {
18501851
check_parent_of_field(self, location, place_base, span, flow_state);
1851-
1852-
// rust-lang/rust#21232, #54499, #54986: during period where we reject
1853-
// partial initialization, do not complain about unnecessary `mut` on
1854-
// an attempt to do a partial initialization.
1855-
self.used_mut.insert(place.local);
18561852
}
18571853

18581854
_ => {}
@@ -1940,6 +1936,11 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
19401936
(prefix, base, span),
19411937
mpi,
19421938
);
1939+
1940+
// rust-lang/rust#21232, #54499, #54986: during period where we reject
1941+
// partial initialization, do not complain about unnecessary `mut` on
1942+
// an attempt to do a partial initialization.
1943+
this.used_mut.insert(base.local);
19431944
}
19441945
}
19451946
}

tests/ui/lint/unused/lint-unused-mut-variables.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,3 +205,11 @@ fn bar() {
205205
let mut b = vec![2]; //~ ERROR: variable does not need to be mutable
206206

207207
}
208+
209+
struct Arg(i32);
210+
211+
// Regression test for https://github.com/rust-lang/rust/issues/110849
212+
fn write_through_reference(mut arg: &mut Arg) {
213+
//~^ WARN: variable does not need to be mutable
214+
arg.0 = 1
215+
}

tests/ui/lint/unused/lint-unused-mut-variables.stderr

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,5 +218,13 @@ note: the lint level is defined here
218218
LL | #[deny(unused_mut)]
219219
| ^^^^^^^^^^
220220

221-
error: aborting due to previous error; 25 warnings emitted
221+
warning: variable does not need to be mutable
222+
--> $DIR/lint-unused-mut-variables.rs:212:28
223+
|
224+
LL | fn write_through_reference(mut arg: &mut Arg) {
225+
| ----^^^
226+
| |
227+
| help: remove this `mut`
228+
229+
error: aborting due to previous error; 26 warnings emitted
222230

0 commit comments

Comments
 (0)