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

Incorrect 'never used' warning for struct when only use is in pattern match #52325

Closed
stearnsc opened this issue Jul 12, 2018 · 5 comments · Fixed by #52332
Closed

Incorrect 'never used' warning for struct when only use is in pattern match #52325

stearnsc opened this issue Jul 12, 2018 · 5 comments · Fixed by #52332

Comments

@stearnsc
Copy link

I ran across this implementing a one-time-use struct for a diesel query.
I incorrectly get a warning:

warning: struct is never used: `Foo`

when compiling

trait SomeTrait {}

struct Foo {
    s: String
}

impl SomeTrait for Foo {}

fn main() {
    let Foo { s } = get_thing();
    println!("{}", s);
}

fn get_thing<T: SomeTrait>() -> T {
    unimplemented!()
}

I got this is on 1.27.0 stable, and verified still exists on 1.29.0-nightly (2018-07-10). I didn't find another issue that looked like the same thing, so hopefully this isn't a duplicate :).

@stearnsc
Copy link
Author

Looks like the warning goes away if the trait impl constructs the struct, which I imagine happens almost always. In my case, the struct was deriving a trait instead of impl'ing something manually. My actual use was something more like:

fn get_something() -> Result<f64, Error> {
    #[derive(QueryableByName)]
    struct Foo { #[sql_type = "Double"] f: f64 }
    let Foo { f } = sql_query("<my query>").get_result(conn)?;
    ...
}

@csmoe
Copy link
Member

csmoe commented Jul 12, 2018

let Foo { s } = get_thing(); You're assigning s with pattern matching here, the Foo isn't' used indeed.

@zackmdavis
Copy link
Member

@stearnsc Thanks for the report! You are right that the issue is that the lint is firing because the struct isn't constructed even if it's used as a match pattern. The message wording was changed for unused enum variants not too long ago for exactly this reason; perhaps we should do the same for structs as well. (Unless someone objects to the consonance of "struct is never constructed"??) Pull request: #52332.

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.
@stearnsc
Copy link
Author

Thank's for the explanation! Is the consensus view that the warning is "correct" then, in that there ought to be a warning for this case? This use of structs doesn't seem particularly hacky or weird to me, so it feels weird that my code should require a fair number of #[allow(dead_code)]s sprinkled throughout in order to compile without warnings.

Or, maybe rephrasing a bit, should the "bug" be considered that the linter doesn't check derived code to see if a struct is constructed?

I haven't looked directly at the derived output, but I'm assuming somewhere in there my type is being explicitly constructed.

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.
@zackmdavis
Copy link
Member

@stearnsc I'm not aware of this having been debated enough for there to be a consensus view. The dead-code analysis lives in src/librustc/middle/dead.rs if you're curious how it works.

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants