Skip to content

Commit 34ef13b

Browse files
authored
Rollup merge of rust-lang#110960 - lukas-code:unused-mut, r=compiler-errors
fix false negative for `unused_mut` fixes rust-lang#110849 We want to avoid double diagnostics for code like this, but only if an error actually occurs: ```rust fn main() { let mut x: (i32, i32); x.0 = 1; } ``` The first commit fixes the lint and the second one removes all the unused `mut`s it found.
2 parents 235d088 + fc63926 commit 34ef13b

File tree

10 files changed

+34
-17
lines changed

10 files changed

+34
-17
lines changed

compiler/rustc_ast_pretty/src/pp.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ impl Printer {
360360

361361
fn check_stack(&mut self, mut depth: usize) {
362362
while let Some(&index) = self.scan_stack.back() {
363-
let mut entry = &mut self.buf[index];
363+
let entry = &mut self.buf[index];
364364
match entry.token {
365365
Token::Begin(_) => {
366366
if depth == 0 {

compiler/rustc_borrowck/src/lib.rs

+6-5
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
}

compiler/rustc_borrowck/src/member_constraints.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ fn append_list(
221221
) {
222222
let mut p = target_list;
223223
loop {
224-
let mut r = &mut constraints[p];
224+
let r = &mut constraints[p];
225225
match r.next_constraint {
226226
Some(q) => p = q,
227227
None => {

compiler/rustc_hir_analysis/src/check/region.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ fn resolve_expr<'tcx>(visitor: &mut RegionResolutionVisitor<'tcx>, expr: &'tcx h
421421
let target_scopes = visitor.fixup_scopes.drain(start_point..);
422422

423423
for scope in target_scopes {
424-
let mut yield_data =
424+
let yield_data =
425425
visitor.scope_tree.yield_in_scope.get_mut(&scope).unwrap().last_mut().unwrap();
426426
let count = yield_data.expr_and_pat_count;
427427
let span = yield_data.span;

compiler/rustc_session/src/options.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -917,7 +917,7 @@ mod parse {
917917
}
918918
}
919919

920-
let mut options = slot.get_or_insert_default();
920+
let options = slot.get_or_insert_default();
921921
let mut seen_always = false;
922922
let mut seen_never = false;
923923
let mut seen_ignore_loops = false;

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

+8
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

+9-1
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

tests/ui/suggestions/suggest-mut-method-for-loop-hashmap.fixed

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ fn main() {
1111
let mut map = HashMap::new();
1212
map.insert("a", Test { v: 0 });
1313

14-
for (_k, mut v) in map.iter_mut() {
14+
for (_k, v) in map.iter_mut() {
1515
//~^ HELP use mutable method
1616
//~| NOTE this iterator yields `&` references
1717
v.v += 1;

tests/ui/suggestions/suggest-mut-method-for-loop-hashmap.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ fn main() {
1111
let mut map = HashMap::new();
1212
map.insert("a", Test { v: 0 });
1313

14-
for (_k, mut v) in map.iter() {
14+
for (_k, v) in map.iter() {
1515
//~^ HELP use mutable method
1616
//~| NOTE this iterator yields `&` references
1717
v.v += 1;

tests/ui/suggestions/suggest-mut-method-for-loop-hashmap.stderr

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
error[E0594]: cannot assign to `v.v`, which is behind a `&` reference
22
--> $DIR/suggest-mut-method-for-loop-hashmap.rs:17:9
33
|
4-
LL | for (_k, mut v) in map.iter() {
5-
| ----------
6-
| | |
7-
| | help: use mutable method: `iter_mut()`
8-
| this iterator yields `&` references
4+
LL | for (_k, v) in map.iter() {
5+
| ----------
6+
| | |
7+
| | help: use mutable method: `iter_mut()`
8+
| this iterator yields `&` references
99
...
1010
LL | v.v += 1;
1111
| ^^^^^^^^ `v` is a `&` reference, so the data it refers to cannot be written

0 commit comments

Comments
 (0)