Skip to content

Incorrect redundant_locals when rebinding to change drop order #11599

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
jrose-signal opened this issue Oct 2, 2023 · 0 comments · Fixed by #11603
Closed

Incorrect redundant_locals when rebinding to change drop order #11599

jrose-signal opened this issue Oct 2, 2023 · 0 comments · Fixed by #11603
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have

Comments

@jrose-signal
Copy link

Summary

When using rebinding to reorder Drops, redundant_locals is emitted, even though the rebinding is not redundant. This is similar to #11320, but happens without any mutation, Copy, or multiple scopes.

Alternatives considered:

  • Use drop explicitly at the end of the function: This code is attempting to be panic-safe, so cleanup should happen in a particular order even in the face of a panic.
  • Use a dedicated scope: The entire rest of the function belongs in that scope, so it'd just add unnecessary indentation.

Lint Name

redundant_locals

Reproducer

I tried this code:

struct DropMe(usize);

impl Drop for DropMe {
    fn drop(&mut self) {
        println!("side effect: {}", self.0)
    }
}

fn main() {
    let first = DropMe(1);
    let second = DropMe(2);
    // Reorder to drop 'first' before 'second'.
    let first = first;
}

I saw this happen:

error: redundant redefinition of a binding
  --> src/main.rs:10:9
   |
10 |     let first = DropMe(1);
   |         ^^^^^
...
13 |     let first = first;
   |     ^^^^^^^^^^^^^^^^^^
   |
   = help: remove the redefinition of `first`
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#redundant_locals
   = note: `#[deny(clippy::redundant_locals)]` on by default

But this suggestion changes the behavior of the program.

Version

Rust 1.72.1, Clippy 0.1.74 (2023-10-01 e0d7ed1) at play.rust-lang.org

Additional Labels

No response

@jrose-signal jrose-signal added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have labels Oct 2, 2023
@bors bors closed this as completed in b437069 Oct 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant