Skip to content

False positive dead_code lint when type is only used via destructuring #132874

@daniel5151

Description

@daniel5151

Code

struct Foo(usize);

fn get_thing<T>() -> T { todo!() }

pub fn foo() {
    let Foo(_x) = get_thing();
    
    // no warning if the type is explicitly used when calling get_thing
    // let Foo(_x) = get_thing::<Foo>();
}

Current output

warning: struct `Foo` is never constructed
 --> src/lib.rs:1:8
  |
1 | struct Foo(usize);
  |        ^^^
  |
  = note: `#[warn(dead_code)]` on by default

Desired output

(nothing)

Rationale and extra context

For context, the specific "get_thing" function I originally ran into this was with zerocopy::FromBytes::read_from_bytes.

Other cases

Rust Version

$ rustc --version --verbose
rustc 1.82.0 (f6e511eec 2024-10-15)
binary: rustc
commit-hash: f6e511eec7342f59a25f7c0534f1dbea00d01b14
commit-date: 2024-10-15
host: x86_64-unknown-linux-gnu
release: 1.82.0
LLVM version: 19.1.1

Anything else?

No response

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.L-dead_codeLint: dead_codeT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions