You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Incorrect 'never used' warning for struct when only use is in pattern match #52325
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 :).
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:
@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.
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.
@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.
Activity
stearnsc commentedon Jul 12, 2018
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:
csmoe commentedon Jul 12, 2018
let Foo { s } = get_thing();You're assigningswith pattern matching here, theFooisn't' used indeed.zackmdavis commentedon Jul 13, 2018
@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.
dead-code lint: say "constructed", "called" for structs, functions
Rollup merge of rust-lang#52332 - zackmdavis:dead_code_lint_should_sa…
stearnsc commentedon Jul 13, 2018
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.
dead-code lint: say "constructed" for structs
zackmdavis commentedon Jul 22, 2018
@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.
Rollup merge of rust-lang#52332 - zackmdavis:dead_code_lint_should_sa…
Rollup merge of rust-lang#52332 - zackmdavis:dead_code_lint_should_sa…
Auto merge of #52332 - zackmdavis:dead_code_lint_should_say_2_electri…