Skip to content

unused_variables gives bad suggestion for nested struct pattern #88403

Closed
@ehuss

Description

@ehuss

I tried this code:

struct Foo {
    bar: Bar,
}

struct Bar {
    f1: i32,
}

fn main() {
    let f = Foo { bar: Bar { f1: 1 } };

    let Foo { bar: Bar { f1 } } = f;   // f1 is unused
}

This generates an unused_variables lint for the pattern. However, the suggestion it gives causes the code to fail to compile:

    let Foo { bar: Bar { _f1 } } = f;

I think it should give the same suggestion it gives for a non-nested binding, like this:

    let Foo { bar: Bar { f1: _ } } = f;

Found during crater run for https://github.com/Kylebrown9/Slang

Meta

rustc --version --verbose:

rustc 1.56.0-nightly (b03ccace5 2021-08-24)
binary: rustc
commit-hash: b03ccace573bb91e27625c190a0f7807045a1012
commit-date: 2021-08-24
host: x86_64-apple-darwin
release: 1.56.0-nightly
LLVM version: 13.0.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`C-bugCategory: This is a bug.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions