]> git.lizzy.rs Git - rust.git/commitdiff
Rollup merge of #55758 - davidtwco:issue-55344, r=pnkfelix
authorMark Rousskov <mark.simulacrum@gmail.com>
Fri, 9 Nov 2018 01:15:10 +0000 (18:15 -0700)
committerGitHub <noreply@github.com>
Fri, 9 Nov 2018 01:15:10 +0000 (18:15 -0700)
[regression - rust2018]: unused_mut lint false positives on nightly

Fixes #55344.

This commit filters out locals that have never been initialized for
consideration in the `unused_mut` lint.

This is intended to detect when the statement that would have
initialized the local was removed as unreachable code. In these cases,
we would not want to lint. This is the same behaviour as the AST borrow
checker.

This is achieved by taking advantage of an existing pass over the MIR
for the `unused_mut` lint and creating a set of those locals that were
never initialized.

r? @pnkfelix

1  2 
src/librustc/mir/mod.rs

diff --combined src/librustc/mir/mod.rs
index c2014a5fdd23a088abb4d599a2df9244c2c7b714,722bb64a7469dd85e23eaac532436894eecaab04..a84226bf665d0df364b6604961f641233c728e1f
@@@ -304,6 -304,20 +304,20 @@@ impl<'tcx> Mir<'tcx> 
          })
      }
  
+     /// Returns an iterator over all user-declared mutable locals.
+     #[inline]
+     pub fn mut_vars_iter<'a>(&'a self) -> impl Iterator<Item = Local> + 'a {
+         (self.arg_count + 1..self.local_decls.len()).filter_map(move |index| {
+             let local = Local::new(index);
+             let decl = &self.local_decls[local];
+             if decl.is_user_variable.is_some() && decl.mutability == Mutability::Mut {
+                 Some(local)
+             } else {
+                 None
+             }
+         })
+     }
      /// Returns an iterator over all user-declared mutable arguments and locals.
      #[inline]
      pub fn mut_vars_and_args_iter<'a>(&'a self) -> impl Iterator<Item = Local> + 'a {
@@@ -506,25 -520,25 +520,25 @@@ pub enum BorrowKind 
      /// implicit closure bindings. It is needed when the closure is
      /// borrowing or mutating a mutable referent, e.g.:
      ///
 -    ///    let x: &mut isize = ...;
 -    ///    let y = || *x += 5;
 +    ///     let x: &mut isize = ...;
 +    ///     let y = || *x += 5;
      ///
      /// If we were to try to translate this closure into a more explicit
      /// form, we'd encounter an error with the code as written:
      ///
 -    ///    struct Env { x: & &mut isize }
 -    ///    let x: &mut isize = ...;
 -    ///    let y = (&mut Env { &x }, fn_ptr);  // Closure is pair of env and fn
 -    ///    fn fn_ptr(env: &mut Env) { **env.x += 5; }
 +    ///     struct Env { x: & &mut isize }
 +    ///     let x: &mut isize = ...;
 +    ///     let y = (&mut Env { &x }, fn_ptr);  // Closure is pair of env and fn
 +    ///     fn fn_ptr(env: &mut Env) { **env.x += 5; }
      ///
      /// This is then illegal because you cannot mutate an `&mut` found
      /// in an aliasable location. To solve, you'd have to translate with
      /// an `&mut` borrow:
      ///
 -    ///    struct Env { x: & &mut isize }
 -    ///    let x: &mut isize = ...;
 -    ///    let y = (&mut Env { &mut x }, fn_ptr); // changed from &x to &mut x
 -    ///    fn fn_ptr(env: &mut Env) { **env.x += 5; }
 +    ///     struct Env { x: & &mut isize }
 +    ///     let x: &mut isize = ...;
 +    ///     let y = (&mut Env { &mut x }, fn_ptr); // changed from &x to &mut x
 +    ///     fn fn_ptr(env: &mut Env) { **env.x += 5; }
      ///
      /// Now the assignment to `**env.x` is legal, but creating a
      /// mutable pointer to `x` is not because `x` is not mutable. We
@@@ -2986,7 -3000,7 +3000,7 @@@ impl<'tcx> TypeFoldable<'tcx> for Termi
          use mir::TerminatorKind::*;
  
          let kind = match self.kind {
 -            Goto { target } => Goto { target: target },
 +            Goto { target } => Goto { target },
              SwitchInt {
                  ref discr,
                  switch_ty,