Skip to content

redundant_locals suggestion may change a closure's trait impls #11562

@kawadakk

Description

@kawadakk

Summary

redundant_locals makes suggestions that would reduce the traits implemented by a closure, causing "unsatisfied trait bounds" errors.

Lint Name

redundant_locals

Reproducer

I tried this code (Playground):

struct AssertSendSync<T>(T); unsafe impl<T> Send for AssertSendSync<T> {} pub fn foo(x: *mut u32) { let x = AssertSendSync(x); bar(move || { let x = x; // move `AssertSendSync` into this closure dbg!(x.0); }); } fn bar(_: impl FnOnce() + Send) {}

I saw this happen:

error: redundant redefinition of a binding --> src/lib.rs:5:9 | 5 | let x = AssertSendSync(x); | ^ 6 | bar(move || { 7 | let x = x; // move `AssertSendSync` into this closure | ^^^^^^^^^^ | = help: remove the redefinition of `x` = 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 

Removing the redefinition of x as suggested results in a compilation error:

error[E0277]: `*mut u32` cannot be sent between threads safely --> src/lib.rs:6:9 | 6 | bar(move || { | --- ^------ | | | | _____|___within this `[closure@src/lib.rs:6:9: 6:16]` | | | | | required by a bound introduced by this call 7 | | // let x = x; // move `AssertSendSync` into this closure 8 | | dbg!(x.0); 9 | | }); | |_____^ `*mut u32` cannot be sent between threads safely | = help: within `[closure@src/lib.rs:6:9: 6:16]`, the trait `Send` is not implemented for `*mut u32` note: required because it's used within this closure --> src/lib.rs:6:9 | 6 | bar(move || { | ^^^^^^^ note: required by a bound in `bar` --> src/lib.rs:12:27 | 12 | fn bar(_: impl FnOnce() + Send) {} | ^^^^ required by this bound in `bar` 

I expected to see this happen:

No diagnostics, or suggesting the dummy let pattern from the relevant edition guide:

- let x = x; // move `AssertSendSync` into this closure + let _ = &x; // move `AssertSendSync` into this closure

Version

rustc 1.74.0-nightly (13e6f24b9 2023-09-23) binary: rustc commit-hash: 13e6f24b9adda67852fb86538541adaa68aff6e8 commit-date: 2023-09-23 host: x86_64-unknown-linux-gnu release: 1.74.0-nightly LLVM version: 17.0.0 

Additional Labels

@rustbot label +I-suggestion-causes-error

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 haveI-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when applied

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions