Skip to content

Commit

Permalink
Rollup merge of rust-lang#61446 - czipperz:nll-unused_mut, r=matthewj…
Browse files Browse the repository at this point in the history
…asper

On TerminatorKind::DropAndReplace still handle unused_mut correctly

Closes rust-lang#61424

- [x] Todo add regression test
  • Loading branch information
Centril authored Jun 4, 2019
2 parents 6ab418a + fea2cdb commit ae4939e
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 14 deletions.
32 changes: 18 additions & 14 deletions src/librustc_mir/borrow_check/used_muts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,19 @@ struct GatherUsedMutsVisitor<'visit, 'cx: 'visit, 'gcx: 'tcx, 'tcx: 'cx> {
mbcx: &'visit mut MirBorrowckCtxt<'cx, 'gcx, 'tcx>,
}

impl GatherUsedMutsVisitor<'_, '_, '_, '_> {
fn remove_never_initialized_mut_locals(&mut self, into: &Place<'_>) {
// Remove any locals that we found were initialized from the
// `never_initialized_mut_locals` set. At the end, the only remaining locals will
// be those that were never initialized - we will consider those as being used as
// they will either have been removed by unreachable code optimizations; or linted
// as unused variables.
if let Some(local) = into.base_local() {
let _ = self.never_initialized_mut_locals.remove(&local);
}
}
}

impl<'visit, 'cx, 'gcx, 'tcx> Visitor<'tcx> for GatherUsedMutsVisitor<'visit, 'cx, 'gcx, 'tcx> {
fn visit_terminator_kind(
&mut self,
Expand All @@ -61,14 +74,10 @@ impl<'visit, 'cx, 'gcx, 'tcx> Visitor<'tcx> for GatherUsedMutsVisitor<'visit, 'c
debug!("visit_terminator_kind: kind={:?}", kind);
match &kind {
TerminatorKind::Call { destination: Some((into, _)), .. } => {
if let Some(local) = into.base_local() {
debug!(
"visit_terminator_kind: kind={:?} local={:?} \
never_initialized_mut_locals={:?}",
kind, local, self.never_initialized_mut_locals
);
let _ = self.never_initialized_mut_locals.remove(&local);
}
self.remove_never_initialized_mut_locals(&into);
},
TerminatorKind::DropAndReplace { location, .. } => {
self.remove_never_initialized_mut_locals(&location);
},
_ => {},
}
Expand All @@ -81,19 +90,14 @@ impl<'visit, 'cx, 'gcx, 'tcx> Visitor<'tcx> for GatherUsedMutsVisitor<'visit, 'c
) {
match &statement.kind {
StatementKind::Assign(into, _) => {
// Remove any locals that we found were initialized from the
// `never_initialized_mut_locals` set. At the end, the only remaining locals will
// be those that were never initialized - we will consider those as being used as
// they will either have been removed by unreachable code optimizations; or linted
// as unused variables.
if let Some(local) = into.base_local() {
debug!(
"visit_statement: statement={:?} local={:?} \
never_initialized_mut_locals={:?}",
statement, local, self.never_initialized_mut_locals
);
let _ = self.never_initialized_mut_locals.remove(&local);
}
self.remove_never_initialized_mut_locals(into);
},
_ => {},
}
Expand Down
7 changes: 7 additions & 0 deletions src/test/ui/nll/issue-61424.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#![deny(unused_mut)]

fn main() {
let mut x; //~ ERROR: variable does not need to be mutable
x = String::new();
dbg!(x);
}
16 changes: 16 additions & 0 deletions src/test/ui/nll/issue-61424.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
error: variable does not need to be mutable
--> $DIR/issue-61424.rs:4:9
|
LL | let mut x;
| ----^
| |
| help: remove this `mut`
|
note: lint level defined here
--> $DIR/issue-61424.rs:1:9
|
LL | #![deny(unused_mut)]
| ^^^^^^^^^^

error: aborting due to previous error

0 comments on commit ae4939e

Please sign in to comment.