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

"variant is never constructed" lint appears to be incorrect? #64362

Closed
iliana opened this issue Sep 10, 2019 · 5 comments · Fixed by #71026
Closed

"variant is never constructed" lint appears to be incorrect? #64362

iliana opened this issue Sep 10, 2019 · 5 comments · Fixed by #71026
Assignees
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

@iliana
Copy link

iliana commented Sep 10, 2019

This code (playground) causes a "variant is never constructed" lint, even though the variant is constructed in the FromStr implementation:

use std::str::FromStr;

enum Foo {
    A { inner: () },
    B,
}

impl FromStr for Foo {
    type Err = ();
    
    fn from_str(s: &str) -> Result<Self, ()> {
        match s {
            "a" => Ok(Self::A { inner: () }),
            "b" => Ok(Self::B),
            _ => Err(()),
        }
    }
}
warning: variant is never constructed: `A`
 --> src/lib.rs:4:5
  |
4 |     A { inner: () },
  |     ^^^^^^^^^^^^^^^
  |
  = note: `#[warn(dead_code)]` on by default

Changing Self for the type name in from_str for Foo (playground) compiles with the "field is never used" lint I would expect from this code.

Tested on rustc 1.39.0-nightly (0b36e9dea 2019-09-09)

This issue has been assigned to @jakubadamw via this comment.

@jonas-schievink jonas-schievink added 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. labels Sep 10, 2019
iliana added a commit to bottlerocket-os/bottlerocket that referenced this issue Sep 10, 2019
Bug report: rust-lang/rust#64362

Signed-off-by: iliana destroyer of worlds <iweller@amazon.com>
@hellow554
Copy link
Contributor

hellow554 commented Sep 11, 2019

This is correct. Your snippet is treated as a library, where the enum Foo is not reachable, because it is not public.
If you write pub enum Foo the error will go away.

Behavoir inteded and therefore this can be closed.

@jakubadamw
Copy link
Contributor

The bug is that it doesn't trigger for B.

If the lint triggered for all variants, then it would just say:

   Compiling playground v0.0.1 (/playground)
warning: enum is never used: `Foo`
 --> src/lib.rs:3:1
  |
3 | enum Foo {
  | ^^^^^^^^
  |
  = note: `#[warn(dead_code)]` on by default

@ztlpn
Copy link
Contributor

ztlpn commented Sep 11, 2019

The problem seems to manifest itself when variants with named fields are constructed using Self::.

Here is a minimal example (playground):

#[derive(Debug)]
enum Foo {
    A { data: u32 },
}

impl Foo {
    fn new() -> Self {
        Self::A { data: 0 }
    }
}

fn main() {
    println!("created: {:?}", Foo::new());
}

@jakubadamw
Copy link
Contributor

@rustbot claim

@rustbot rustbot self-assigned this Sep 11, 2019
Centril added a commit to Centril/rust that referenced this issue Sep 14, 2019
Fix false "never constructed" warnings for `Self::` variant paths

Closes rust-lang#64362.
iliana added a commit to awslabs/tough that referenced this issue Nov 5, 2019
Bug report: rust-lang/rust#64362

Signed-off-by: iliana destroyer of worlds <iweller@amazon.com>
iliana added a commit to awslabs/tough that referenced this issue Nov 5, 2019
Bug report: rust-lang/rust#64362

Signed-off-by: iliana destroyer of worlds <iweller@amazon.com>
iliana added a commit to awslabs/tough that referenced this issue Nov 8, 2019
Bug report: rust-lang/rust#64362

Signed-off-by: iliana destroyer of worlds <iweller@amazon.com>
iliana added a commit to awslabs/tough that referenced this issue Nov 8, 2019
Bug report: rust-lang/rust#64362

Signed-off-by: iliana destroyer of worlds <iweller@amazon.com>
@hjmallon
Copy link

hjmallon commented Dec 4, 2019

Here is a slightly altered playground to illlustrate further.

https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=3658bbcaddc011fee8744d027a2086ad

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
7 participants