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

dead code lint should say "never constructed" for variants that are matched on but never constructed #19140

Closed
pnkfelix opened this issue Nov 20, 2014 · 5 comments
Labels
A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. C-enhancement Category: An issue proposing an enhancement or a PR with one.

Comments

@pnkfelix
Copy link
Member

mod a {
    #[deriving(Show)]
    pub enum E {
        Variant1,
        Variant2,
    }
}

fn main() {
    let x = a::E::Variant1;
    let y = match x {
        a::E::Variant1 => 1i,
        a::E::Variant2 => 2i,
    };
    println!("Hello world: {}", (x, y));
}

produces:

<anon>:5:9: 5:17 warning: variant is never used: `Variant2`, #[warn(dead_code)] on by default
<anon>:5         Variant2,
                 ^~~~~~~~
@pnkfelix
Copy link
Member Author

(arguably this may be a feature, since the arm associated with the pattern here is nontheless never used.)

@ghost
Copy link

ghost commented Nov 22, 2014

@pnkfelix Yeah, this was intentional although I'm going to reopen this as I think we can do better on the wording. Used is indeed misleading, constructed is what the lint is proving.

@ghost ghost reopened this Nov 22, 2014
@ghost ghost changed the title dead code lint is warning about variants that are used as patterns dead code lint should say "never constructed" for variants that are matched on but never constructed Nov 22, 2014
@sanxiyn sanxiyn added the A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. label Jan 25, 2015
@steveklabnik
Copy link
Member

Triage: still an issue today. Updated code:

mod a {
    #[derive(Debug)]
    pub enum E {
        Variant1,
        Variant2,
    }
}

fn main() {
    let x = a::E::Variant1;
    let y = match x {
        a::E::Variant1 => 1,
        a::E::Variant2 => 2,
    };
    println!("Hello world: {:?}", (x, y));
}

@nox
Copy link
Contributor

nox commented Sep 14, 2017

Could we get some action on that? Seems important, with all the ergonomics stuff going on.

zackmdavis added a commit to zackmdavis/rust that referenced this issue Nov 19, 2017
As reported in rust-lang#19140, rust-lang#44083, and rust-lang#44565, some users were confused when
the dead-code lint reported an enum variant to be "unused" when it was
matched on (but not constructed). This wording change makes it clearer
that the lint is in fact checking for construction.

We continue to say "used" for all other items (it's tempting to say
"called" for functions and methods, but this turns out not to be
correct: functions can be passed as arguments and the dead-code lint
isn't special-casing that or anything).

Resolves rust-lang#19140.
kennytm added a commit to kennytm/rust that referenced this issue Nov 21, 2017
…y_never_constructed_for_variants, r=arielb1

dead code lint to say "never constructed" for variants

As reported in rust-lang#19140, rust-lang#44083, and rust-lang#44565, some users were confused when
the dead-code lint reported an enum variant to be "unused" when it was
matched on (but not constructed). This wording change makes it clearer
that the lint is in fact checking for construction.

We continue to say "used" for all other items (it's tempting to say
"called" for functions and methods, but this turns out not to be
correct: functions can be passed as arguments and the dead-code lint
isn't special-casing that or anything).

Resolves rust-lang#19140.

r? @pnkfelix
zackmdavis added a commit to zackmdavis/rust that referenced this issue Jul 13, 2018
Respectively.

This is a sequel to November 2017's rust-lang#46103 / 1a9dc2e. It had been
reported (more than once—at least rust-lang#19140, rust-lang#44083, and rust-lang#44565) that the
"never used" language was confusing for enum variants that were "used"
as match patterns, so the wording was changed to say never "constructed"
specifically for enum variants. More recently, the same issue was raised
for structs (rust-lang#52325). It seems consistent to say "constructed" here,
too, for the same reasons.

While we're here, we can also use more specific word "called" for unused
functions and methods. (We declined to do this in rust-lang#46103, but the
rationale given in the commit message doesn't actually make sense.)

This resolves rust-lang#52325.
Mark-Simulacrum added a commit to Mark-Simulacrum/rust that referenced this issue Jul 13, 2018
…y_2_electric_boogaloo, r=pnkfelix

dead-code lint: say "constructed", "called" for structs, functions

Respectively.

This is a sequel to November 2017's rust-lang#46103 / 1a9dc2e. It had been
reported (more than once—at least rust-lang#19140, rust-lang#44083, and rust-lang#44565) that the
"never used" language was confusing for enum variants that were "used"
as match patterns, so the wording was changed to say never "constructed"
specifically for enum variants. More recently, the same issue was raised
for structs (rust-lang#52325). It seems consistent to say "constructed" here,
too, for the same reasons.

While we're here, we can also use more specific word "called" for unused
functions and methods. (We declined to do this in rust-lang#46103, but the
rationale given in the commit message doesn't actually make sense.)

This resolves rust-lang#52325.
zackmdavis added a commit to zackmdavis/rust that referenced this issue Jul 22, 2018
This is a sequel to November 2017's rust-lang#46103 / 1a9dc2e. It had been
reported (more than once—at least rust-lang#19140, rust-lang#44083, and rust-lang#44565) that the
"never used" language was confusing for enum variants that were "used"
as match patterns, so the wording was changed to say never
"constructed" specifically for enum variants. More recently, the same
issue was raised for structs (rust-lang#52325). It seems consistent to say
"constructed" here, too, for the same reasons.

We considered using more specific word "called" for unused functions
and methods (while we declined to do this in rust-lang#46103, the rationale
given in the commit message doesn't actually make sense), but it turns
out that Cargo's test suite expects the "never used" message, and
maybe we don't care enough even to make a Cargo PR over such a petty
and subjective wording change.

This resolves rust-lang#52325.
GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this issue Jul 31, 2018
…y_2_electric_boogaloo, r=pnkfelix

dead-code lint: say "constructed" for structs

Respectively.

This is a sequel to November 2017's rust-lang#46103 / 1a9dc2e. It had been
reported (more than once—at least rust-lang#19140, rust-lang#44083, and rust-lang#44565) that the
"never used" language was confusing for enum variants that were "used"
as match patterns, so the wording was changed to say never "constructed"
specifically for enum variants. More recently, the same issue was raised
for structs (rust-lang#52325). It seems consistent to say "constructed" here,
too, for the same reasons.

~~While we're here, we can also use more specific word "called" for unused
functions and methods. (We declined to do this in rust-lang#46103, but the
rationale given in the commit message doesn't actually make sense.)~~

This resolves rust-lang#52325.
Mark-Simulacrum added a commit to Mark-Simulacrum/rust that referenced this issue Aug 1, 2018
…y_2_electric_boogaloo, r=pnkfelix

dead-code lint: say "constructed" for structs

Respectively.

This is a sequel to November 2017's rust-lang#46103 / 1a9dc2e. It had been
reported (more than once—at least rust-lang#19140, rust-lang#44083, and rust-lang#44565) that the
"never used" language was confusing for enum variants that were "used"
as match patterns, so the wording was changed to say never "constructed"
specifically for enum variants. More recently, the same issue was raised
for structs (rust-lang#52325). It seems consistent to say "constructed" here,
too, for the same reasons.

~~While we're here, we can also use more specific word "called" for unused
functions and methods. (We declined to do this in rust-lang#46103, but the
rationale given in the commit message doesn't actually make sense.)~~

This resolves rust-lang#52325.
bors added a commit that referenced this issue Aug 6, 2018
…c_boogaloo, r=pnkfelix

dead-code lint: say "constructed" for structs

Respectively.

This is a sequel to November 2017's #46103 / 1a9dc2e. It had been
reported (more than once—at least #19140, #44083, and #44565) that the
"never used" language was confusing for enum variants that were "used"
as match patterns, so the wording was changed to say never "constructed"
specifically for enum variants. More recently, the same issue was raised
for structs (#52325). It seems consistent to say "constructed" here,
too, for the same reasons.

~~While we're here, we can also use more specific word "called" for unused
functions and methods. (We declined to do this in #46103, but the
rationale given in the commit message doesn't actually make sense.)~~

This resolves #52325.
@JohnyL
Copy link

JohnyL commented Aug 16, 2021

Any news on fixing this?

enum Mode {
    Open,
    Close
}

fn main() {
    let mode = Mode::Open;
    let num = match mode {
        Mode::Open => 1,
        Mode::Close => 2
    };
}

Warning:

warning: variant is never constructed: Close
--> src\main.rs:3:5
|
3 | Close
| ^^^^^
|
= note: #[warn(dead_code)] on by default

warning: 1 warning emitted

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-enhancement Category: An issue proposing an enhancement or a PR with one.
Projects
None yet
Development

No branches or pull requests

6 participants