Skip to content

False positive on dead_code lint #68408

Closed

Description

I'm sure that there is a more concise way to generate this error, but this produces a dead_code lint warning:

#![deny(warnings)]

use std::mem;

#[derive(Default)]
struct Y {}
enum X {
    A { y: Y },
    B { y: Y },
}
impl X {
    fn test(&mut self) {
        if let Self::A { y } = self {
            *self = Self::B { y: mem::take(y) };
        }
    }
}
pub fn main() {
    let mut x = X::A { y: Y {} };
    x.test();
}

(Playground)

This code instantiates X::B, but the dead_code lint disagrees:

   Compiling playground v0.0.1 (/playground)
error: variant is never constructed: `B`
 --> src/main.rs:9:5
  |
9 |     B { y: Y },
  |     ^^^^^^^^^^
  |
note: lint level defined here
 --> src/main.rs:1:9
  |
1 | #![deny(warnings)]
  |         ^^^^^^^^
  = note: `#[deny(dead_code)]` implied by `#[deny(warnings)]`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    A-lintArea: Lints (warnings about flaws in source code) such as unused_mut.C-bugCategory: This is a bug.E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.T-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