Skip to content

Commit

Permalink
Rollup merge of rust-lang#52810 - matthewjasper:more-immutablity, r=p…
Browse files Browse the repository at this point in the history
…nkfelix

[NLL] Don't make "fake" match variables mutable

These variables can't be mutated by the user, but since they have names the unused-mut lint thinks that it should check them.
  • Loading branch information
pietroalbini authored Aug 1, 2018
2 parents 714fd37 + 173c330 commit 333d8c4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/librustc_mir/build/matches/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1213,11 +1213,17 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
let locals = if has_guard.0 && tcx.all_pat_vars_are_implicit_refs_within_guards() {
let mut vals_for_guard = Vec::with_capacity(num_patterns);
for _ in 0..num_patterns {
let val_for_guard_idx = self.local_decls.push(local.clone());
let val_for_guard_idx = self.local_decls.push(LocalDecl {
// This variable isn't mutated but has a name, so has to be
// immutable to avoid the unused mut lint.
mutability: Mutability::Not,
..local.clone()
});
vals_for_guard.push(val_for_guard_idx);
}
let ref_for_guard = self.local_decls.push(LocalDecl::<'tcx> {
mutability,
// See previous comment.
mutability: Mutability::Not,
ty: tcx.mk_imm_ref(tcx.types.re_empty, var_ty),
name: Some(name),
source_info,
Expand Down
9 changes: 9 additions & 0 deletions src/test/ui/nll/extra-unused-mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,19 @@ fn parse_dot_or_call_expr_with(mut attrs: Vec<u32>) {
);
}

// Found when trying to bootstrap rustc
fn if_guard(x: Result<i32, i32>) {
match x {
Ok(mut r) | Err(mut r) if true => r = 1,
_ => (),
}
}

fn main() {
ref_argument(0);
mutable_upvar();
generator_mutable_upvar();
ref_closure_argument();
parse_dot_or_call_expr_with(Vec::new());
if_guard(Ok(0));
}

0 comments on commit 333d8c4

Please sign in to comment.