From 173c33019e0daf982d64bc6658f4aca5b0dac419 Mon Sep 17 00:00:00 2001 From: Matthew Jasper Date: Sat, 28 Jul 2018 21:18:34 +0100 Subject: [PATCH] Don't make "fake" match variables mutable --- src/librustc_mir/build/matches/mod.rs | 10 ++++++++-- src/test/ui/nll/extra-unused-mut.rs | 9 +++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/librustc_mir/build/matches/mod.rs b/src/librustc_mir/build/matches/mod.rs index a509ec08a142e..6a447d81dc3cb 100644 --- a/src/librustc_mir/build/matches/mod.rs +++ b/src/librustc_mir/build/matches/mod.rs @@ -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, diff --git a/src/test/ui/nll/extra-unused-mut.rs b/src/test/ui/nll/extra-unused-mut.rs index ce07e2b0e2184..472ac2cf1e84c 100644 --- a/src/test/ui/nll/extra-unused-mut.rs +++ b/src/test/ui/nll/extra-unused-mut.rs @@ -55,10 +55,19 @@ fn parse_dot_or_call_expr_with(mut attrs: Vec) { ); } +// Found when trying to bootstrap rustc +fn if_guard(x: Result) { + 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)); }