about summary refs log tree commit diff
diff options
context:
space:
mode:
authorPietro Albini <pietro@pietroalbini.org>2018-08-01 10:12:41 +0200
committerGitHub <noreply@github.com>2018-08-01 10:12:41 +0200
commit333d8c456c507cdbf31f790d56524126ae112e0d (patch)
tree5c9803442edecdc3a5adc43c1e4412bd260c3ece
parent714fd37b8ba17f1129f7429e21321edb11605955 (diff)
parent173c33019e0daf982d64bc6658f4aca5b0dac419 (diff)
downloadrust-333d8c456c507cdbf31f790d56524126ae112e0d.tar.gz
rust-333d8c456c507cdbf31f790d56524126ae112e0d.zip
Rollup merge of #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.
-rw-r--r--src/librustc_mir/build/matches/mod.rs10
-rw-r--r--src/test/ui/nll/extra-unused-mut.rs9
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 a509ec08a14..6a447d81dc3 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 ce07e2b0e21..472ac2cf1e8 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<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));
 }