Skip to content
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

False dead_code warning on struct pattern match #56750

Open
nvzqz opened this issue Dec 12, 2018 · 4 comments
Open

False dead_code warning on struct pattern match #56750

nvzqz opened this issue Dec 12, 2018 · 4 comments
Labels
A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@nvzqz
Copy link
Contributor

nvzqz commented Dec 12, 2018

The following code will emit a dead_code warning (playground):

use std::mem;

fn main() {
    struct Value {
        a: i32,
        b: i32,
    };

    let Value { a, b } = unsafe { mem::zeroed() };
    println!("{} {}", a, b);
}

It states that Value is never constructed when in fact it has been within the let Value { a, b } pattern match.

   Compiling playground v0.0.1 (/playground)
warning: struct is never constructed: `Value`
 --> src/main.rs:4:5
  |
4 |     struct Value {
  |     ^^^^^^^^^^^^
  |
  = note: #[warn(dead_code)] on by default

    Finished dev [unoptimized + debuginfo] target(s) in 0.90s
     Running `target/debug/playground`
nvzqz added a commit to oceanpkg/os-utils that referenced this issue Dec 12, 2018
@estebank estebank added A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. C-bug Category: This is a bug. labels Dec 12, 2018
@zackmdavis
Copy link
Member

@nvzqz Thanks for the report! We recently changed the wording to say "never constructed" rather than "never used" in the hopes of clarifying that the liveness analysis doesn't consider destructuring patterns as "live," but the existence of this Issue would seem to suggest that this still isn't clear enough. 🙁

Part of me was tempted to call this #wontfix (as it wasn't obvious to me why it's useful to have a struct pattern without the struct being constructed anywhere else—what would realistically be on the other side of the match/assignment?), but your linked commit being a real-world counterexample makes me more sympathetic to changing the analysis to consider patterns "live." (As you point out, unsafe bytes can be on the other side of the match, and you might want to have a struct to name them.)

@nvzqz
Copy link
Contributor Author

nvzqz commented Dec 14, 2018

I noticed the warning for Value not being constructed but in my reasoning, it actually is when returned from mem::zeroed() and then immediately deconstructed in the pattern. I know that's a very implicit way of viewing it. I still think that code like this that's correct shouldn't emit warnings despite the warning being generated in unsafe code.

Thanks for taking my real-world case in that commit into account!

@danielhenrymantilla
Copy link
Contributor

Using a struct only for it to be destructured right away does indeed have its use cases, see also: https://github.com/danielhenrymantilla/fstrings-rs/blob/2de1a3db8d6bf6f9e33ea6eeef8468d75b329c83/src/proc_macro/mod.rs#L91-L94:

let Args {
    format_literal,
    mut extra_args,
} = parse_macro_input!(input); // Args : Parse
  • My Args struct implements Parse logic, but then I just want to extract its fields right away.

  • Thus my Args struct is definitely not dead_code.

@Spoonbender
Copy link

Triage: no change

@Enselic Enselic added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Aug 17, 2023
dscho added a commit to dscho/lychee that referenced this issue May 12, 2024
Clippy v0.1.78 identifies this as dead code. However, further down in
the same file, there is clearly a user:

  impl Handler<Result, Result> for Add {

This might be yet another incarnation of
rust-lang/rust#56750

Let's just mark it as intentionally dead-code, even if this is untrue,
to make clippy happy again.

Signed-off-by: Johannes Schindelin <[email protected]>
dscho added a commit to dscho/lychee that referenced this issue May 12, 2024
Clippy v0.1.78 identifies this as dead code. However, further down in
the same file, there is clearly a user:

  impl Handler<Result, Result> for Add {

This might be yet another incarnation of
rust-lang/rust#56750

Let's just mark it as intentionally dead-code, even if this is untrue,
to make clippy happy again.

Signed-off-by: Johannes Schindelin <[email protected]>
mre pushed a commit to lycheeverse/lychee that referenced this issue May 13, 2024
* Enclose Markdown links in brackets

The current clippy version (v0.1.78) says "you should put bare URLs
between `<`/`>` or make a proper Markdown link" and refers to
https://rust-lang.github.io/rust-clippy/master/index.html#doc_markdown

Signed-off-by: Johannes Schindelin <[email protected]>

* Enclose documentation item in backticks

Clippy v0.1.78 complains about the IPv6 network mask, insisting that it
is missing backticks. So backticks it gets.

Signed-off-by: Johannes Schindelin <[email protected]>

* Avoid error claiming `Add(usize)` is dead code

Clippy v0.1.78 identifies this as dead code. However, further down in
the same file, there is clearly a user:

  impl Handler<Result, Result> for Add {

This might be yet another incarnation of
rust-lang/rust#56750

Let's just mark it as intentionally dead-code, even if this is untrue,
to make clippy happy again.

Signed-off-by: Johannes Schindelin <[email protected]>

---------

Signed-off-by: Johannes Schindelin <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

6 participants