Skip to content

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

@iliana

Description

@iliana

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.

Activity

added
A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.
C-bugCategory: This is a bug.
T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
on Sep 10, 2019
hellow554

hellow554 commented on Sep 11, 2019

@hellow554
Contributor

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

jakubadamw commented on Sep 11, 2019

@jakubadamw
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

ztlpn commented on Sep 11, 2019

@ztlpn
Contributor

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

jakubadamw commented on Sep 11, 2019

@jakubadamw
Contributor

@rustbot claim

self-assigned this
on Sep 11, 2019
added a commit that references this issue on Sep 14, 2019
4d32778
hjmallon

hjmallon commented on Dec 4, 2019

@hjmallon
added a commit that references this issue on Dec 15, 2019
added a commit that references this issue on Apr 19, 2020
9c5da9a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.C-bugCategory: This is a bug.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

    Participants

    @iliana@hellow554@jonas-schievink@hjmallon@ztlpn

    Issue actions

      "variant is never constructed" lint appears to be incorrect? · Issue #64362 · rust-lang/rust