Skip to content

Incorrect redundant_locals when rebinding to change drop order #11599

Closed
@jrose-signal

Description

@jrose-signal

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't have

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions