Skip to content

Commit f8a2363

Browse files
authored
Rollup merge of rust-lang#61446 - czipperz:nll-unused_mut, r=matthewjasper
On TerminatorKind::DropAndReplace still handle unused_mut correctly Closes rust-lang#61424 - [x] Todo add regression test
2 parents 28ce2b1 + fea2cdb commit f8a2363

File tree

3 files changed

+41
-14
lines changed

3 files changed

+41
-14
lines changed

src/librustc_mir/borrow_check/used_muts.rs

+18-14
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,19 @@ struct GatherUsedMutsVisitor<'visit, 'cx: 'visit, 'gcx: 'tcx, 'tcx: 'cx> {
5252
mbcx: &'visit mut MirBorrowckCtxt<'cx, 'gcx, 'tcx>,
5353
}
5454

55+
impl GatherUsedMutsVisitor<'_, '_, '_, '_> {
56+
fn remove_never_initialized_mut_locals(&mut self, into: &Place<'_>) {
57+
// Remove any locals that we found were initialized from the
58+
// `never_initialized_mut_locals` set. At the end, the only remaining locals will
59+
// be those that were never initialized - we will consider those as being used as
60+
// they will either have been removed by unreachable code optimizations; or linted
61+
// as unused variables.
62+
if let Some(local) = into.base_local() {
63+
let _ = self.never_initialized_mut_locals.remove(&local);
64+
}
65+
}
66+
}
67+
5568
impl<'visit, 'cx, 'gcx, 'tcx> Visitor<'tcx> for GatherUsedMutsVisitor<'visit, 'cx, 'gcx, 'tcx> {
5669
fn visit_terminator_kind(
5770
&mut self,
@@ -61,14 +74,10 @@ impl<'visit, 'cx, 'gcx, 'tcx> Visitor<'tcx> for GatherUsedMutsVisitor<'visit, 'c
6174
debug!("visit_terminator_kind: kind={:?}", kind);
6275
match &kind {
6376
TerminatorKind::Call { destination: Some((into, _)), .. } => {
64-
if let Some(local) = into.base_local() {
65-
debug!(
66-
"visit_terminator_kind: kind={:?} local={:?} \
67-
never_initialized_mut_locals={:?}",
68-
kind, local, self.never_initialized_mut_locals
69-
);
70-
let _ = self.never_initialized_mut_locals.remove(&local);
71-
}
77+
self.remove_never_initialized_mut_locals(&into);
78+
},
79+
TerminatorKind::DropAndReplace { location, .. } => {
80+
self.remove_never_initialized_mut_locals(&location);
7281
},
7382
_ => {},
7483
}
@@ -81,19 +90,14 @@ impl<'visit, 'cx, 'gcx, 'tcx> Visitor<'tcx> for GatherUsedMutsVisitor<'visit, 'c
8190
) {
8291
match &statement.kind {
8392
StatementKind::Assign(into, _) => {
84-
// Remove any locals that we found were initialized from the
85-
// `never_initialized_mut_locals` set. At the end, the only remaining locals will
86-
// be those that were never initialized - we will consider those as being used as
87-
// they will either have been removed by unreachable code optimizations; or linted
88-
// as unused variables.
8993
if let Some(local) = into.base_local() {
9094
debug!(
9195
"visit_statement: statement={:?} local={:?} \
9296
never_initialized_mut_locals={:?}",
9397
statement, local, self.never_initialized_mut_locals
9498
);
95-
let _ = self.never_initialized_mut_locals.remove(&local);
9699
}
100+
self.remove_never_initialized_mut_locals(into);
97101
},
98102
_ => {},
99103
}

src/test/ui/nll/issue-61424.rs

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#![deny(unused_mut)]
2+
3+
fn main() {
4+
let mut x; //~ ERROR: variable does not need to be mutable
5+
x = String::new();
6+
dbg!(x);
7+
}

src/test/ui/nll/issue-61424.stderr

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
error: variable does not need to be mutable
2+
--> $DIR/issue-61424.rs:4:9
3+
|
4+
LL | let mut x;
5+
| ----^
6+
| |
7+
| help: remove this `mut`
8+
|
9+
note: lint level defined here
10+
--> $DIR/issue-61424.rs:1:9
11+
|
12+
LL | #![deny(unused_mut)]
13+
| ^^^^^^^^^^
14+
15+
error: aborting due to previous error
16+

0 commit comments

Comments
 (0)