Rollup merge of #110960 - lukas-code:unused-mut, r=compiler-errors
fix false negative for `unused_mut` fixes https://github.com/rust-lang/rust/issues/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.
This commit is contained in:
commit
34ef13b15b
10 changed files with 34 additions and 17 deletions
|
@ -360,7 +360,7 @@ impl Printer {
|
||||||
|
|
||||||
fn check_stack(&mut self, mut depth: usize) {
|
fn check_stack(&mut self, mut depth: usize) {
|
||||||
while let Some(&index) = self.scan_stack.back() {
|
while let Some(&index) = self.scan_stack.back() {
|
||||||
let mut entry = &mut self.buf[index];
|
let entry = &mut self.buf[index];
|
||||||
match entry.token {
|
match entry.token {
|
||||||
Token::Begin(_) => {
|
Token::Begin(_) => {
|
||||||
if depth == 0 {
|
if depth == 0 {
|
||||||
|
|
|
@ -935,6 +935,7 @@ enum InitializationRequiringAction {
|
||||||
PartialAssignment,
|
PartialAssignment,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
struct RootPlace<'tcx> {
|
struct RootPlace<'tcx> {
|
||||||
place_local: Local,
|
place_local: Local,
|
||||||
place_projection: &'tcx [PlaceElem<'tcx>],
|
place_projection: &'tcx [PlaceElem<'tcx>],
|
||||||
|
@ -1848,11 +1849,6 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
|
||||||
// is allowed, remove this match arm.
|
// is allowed, remove this match arm.
|
||||||
ty::Adt(..) | ty::Tuple(..) => {
|
ty::Adt(..) | ty::Tuple(..) => {
|
||||||
check_parent_of_field(self, location, place_base, span, flow_state);
|
check_parent_of_field(self, location, place_base, span, flow_state);
|
||||||
|
|
||||||
// rust-lang/rust#21232, #54499, #54986: during period where we reject
|
|
||||||
// partial initialization, do not complain about unnecessary `mut` on
|
|
||||||
// an attempt to do a partial initialization.
|
|
||||||
self.used_mut.insert(place.local);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_ => {}
|
_ => {}
|
||||||
|
@ -1940,6 +1936,11 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
|
||||||
(prefix, base, span),
|
(prefix, base, span),
|
||||||
mpi,
|
mpi,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// rust-lang/rust#21232, #54499, #54986: during period where we reject
|
||||||
|
// partial initialization, do not complain about unnecessary `mut` on
|
||||||
|
// an attempt to do a partial initialization.
|
||||||
|
this.used_mut.insert(base.local);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -221,7 +221,7 @@ fn append_list(
|
||||||
) {
|
) {
|
||||||
let mut p = target_list;
|
let mut p = target_list;
|
||||||
loop {
|
loop {
|
||||||
let mut r = &mut constraints[p];
|
let r = &mut constraints[p];
|
||||||
match r.next_constraint {
|
match r.next_constraint {
|
||||||
Some(q) => p = q,
|
Some(q) => p = q,
|
||||||
None => {
|
None => {
|
||||||
|
|
|
@ -421,7 +421,7 @@ fn resolve_expr<'tcx>(visitor: &mut RegionResolutionVisitor<'tcx>, expr: &'tcx h
|
||||||
let target_scopes = visitor.fixup_scopes.drain(start_point..);
|
let target_scopes = visitor.fixup_scopes.drain(start_point..);
|
||||||
|
|
||||||
for scope in target_scopes {
|
for scope in target_scopes {
|
||||||
let mut yield_data =
|
let yield_data =
|
||||||
visitor.scope_tree.yield_in_scope.get_mut(&scope).unwrap().last_mut().unwrap();
|
visitor.scope_tree.yield_in_scope.get_mut(&scope).unwrap().last_mut().unwrap();
|
||||||
let count = yield_data.expr_and_pat_count;
|
let count = yield_data.expr_and_pat_count;
|
||||||
let span = yield_data.span;
|
let span = yield_data.span;
|
||||||
|
|
|
@ -917,7 +917,7 @@ mod parse {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut options = slot.get_or_insert_default();
|
let options = slot.get_or_insert_default();
|
||||||
let mut seen_always = false;
|
let mut seen_always = false;
|
||||||
let mut seen_never = false;
|
let mut seen_never = false;
|
||||||
let mut seen_ignore_loops = false;
|
let mut seen_ignore_loops = false;
|
||||||
|
|
|
@ -205,3 +205,11 @@ fn bar() {
|
||||||
let mut b = vec![2]; //~ ERROR: variable does not need to be mutable
|
let mut b = vec![2]; //~ ERROR: variable does not need to be mutable
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct Arg(i32);
|
||||||
|
|
||||||
|
// Regression test for https://github.com/rust-lang/rust/issues/110849
|
||||||
|
fn write_through_reference(mut arg: &mut Arg) {
|
||||||
|
//~^ WARN: variable does not need to be mutable
|
||||||
|
arg.0 = 1
|
||||||
|
}
|
||||||
|
|
|
@ -218,5 +218,13 @@ note: the lint level is defined here
|
||||||
LL | #[deny(unused_mut)]
|
LL | #[deny(unused_mut)]
|
||||||
| ^^^^^^^^^^
|
| ^^^^^^^^^^
|
||||||
|
|
||||||
error: aborting due to previous error; 25 warnings emitted
|
warning: variable does not need to be mutable
|
||||||
|
--> $DIR/lint-unused-mut-variables.rs:212:28
|
||||||
|
|
|
||||||
|
LL | fn write_through_reference(mut arg: &mut Arg) {
|
||||||
|
| ----^^^
|
||||||
|
| |
|
||||||
|
| help: remove this `mut`
|
||||||
|
|
||||||
|
error: aborting due to previous error; 26 warnings emitted
|
||||||
|
|
||||||
|
|
|
@ -11,7 +11,7 @@ fn main() {
|
||||||
let mut map = HashMap::new();
|
let mut map = HashMap::new();
|
||||||
map.insert("a", Test { v: 0 });
|
map.insert("a", Test { v: 0 });
|
||||||
|
|
||||||
for (_k, mut v) in map.iter_mut() {
|
for (_k, v) in map.iter_mut() {
|
||||||
//~^ HELP use mutable method
|
//~^ HELP use mutable method
|
||||||
//~| NOTE this iterator yields `&` references
|
//~| NOTE this iterator yields `&` references
|
||||||
v.v += 1;
|
v.v += 1;
|
||||||
|
|
|
@ -11,7 +11,7 @@ fn main() {
|
||||||
let mut map = HashMap::new();
|
let mut map = HashMap::new();
|
||||||
map.insert("a", Test { v: 0 });
|
map.insert("a", Test { v: 0 });
|
||||||
|
|
||||||
for (_k, mut v) in map.iter() {
|
for (_k, v) in map.iter() {
|
||||||
//~^ HELP use mutable method
|
//~^ HELP use mutable method
|
||||||
//~| NOTE this iterator yields `&` references
|
//~| NOTE this iterator yields `&` references
|
||||||
v.v += 1;
|
v.v += 1;
|
||||||
|
|
|
@ -1,11 +1,11 @@
|
||||||
error[E0594]: cannot assign to `v.v`, which is behind a `&` reference
|
error[E0594]: cannot assign to `v.v`, which is behind a `&` reference
|
||||||
--> $DIR/suggest-mut-method-for-loop-hashmap.rs:17:9
|
--> $DIR/suggest-mut-method-for-loop-hashmap.rs:17:9
|
||||||
|
|
|
|
||||||
LL | for (_k, mut v) in map.iter() {
|
LL | for (_k, v) in map.iter() {
|
||||||
| ----------
|
| ----------
|
||||||
| | |
|
| | |
|
||||||
| | help: use mutable method: `iter_mut()`
|
| | help: use mutable method: `iter_mut()`
|
||||||
| this iterator yields `&` references
|
| this iterator yields `&` references
|
||||||
...
|
...
|
||||||
LL | v.v += 1;
|
LL | v.v += 1;
|
||||||
| ^^^^^^^^ `v` is a `&` reference, so the data it refers to cannot be written
|
| ^^^^^^^^ `v` is a `&` reference, so the data it refers to cannot be written
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue