Skip to content

Commit ce005b5

Browse files
Rollup merge of rust-lang#52810 - matthewjasper:more-immutablity, r=pnkfelix
[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.
2 parents eca1912 + 173c330 commit ce005b5

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

src/librustc_mir/build/matches/mod.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -1213,11 +1213,17 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
12131213
let locals = if has_guard.0 && tcx.all_pat_vars_are_implicit_refs_within_guards() {
12141214
let mut vals_for_guard = Vec::with_capacity(num_patterns);
12151215
for _ in 0..num_patterns {
1216-
let val_for_guard_idx = self.local_decls.push(local.clone());
1216+
let val_for_guard_idx = self.local_decls.push(LocalDecl {
1217+
// This variable isn't mutated but has a name, so has to be
1218+
// immutable to avoid the unused mut lint.
1219+
mutability: Mutability::Not,
1220+
..local.clone()
1221+
});
12171222
vals_for_guard.push(val_for_guard_idx);
12181223
}
12191224
let ref_for_guard = self.local_decls.push(LocalDecl::<'tcx> {
1220-
mutability,
1225+
// See previous comment.
1226+
mutability: Mutability::Not,
12211227
ty: tcx.mk_imm_ref(tcx.types.re_empty, var_ty),
12221228
name: Some(name),
12231229
source_info,

src/test/ui/nll/extra-unused-mut.rs

+9
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,19 @@ fn parse_dot_or_call_expr_with(mut attrs: Vec<u32>) {
5555
);
5656
}
5757

58+
// Found when trying to bootstrap rustc
59+
fn if_guard(x: Result<i32, i32>) {
60+
match x {
61+
Ok(mut r) | Err(mut r) if true => r = 1,
62+
_ => (),
63+
}
64+
}
65+
5866
fn main() {
5967
ref_argument(0);
6068
mutable_upvar();
6169
generator_mutable_upvar();
6270
ref_closure_argument();
6371
parse_dot_or_call_expr_with(Vec::new());
72+
if_guard(Ok(0));
6473
}

0 commit comments

Comments
 (0)