-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
impls should not inhibit the unused_item lint #47851
Comments
Does anyone have any suggestions on what part of the code to look at to attempt to fix this? |
The warning message is: enum Foo {} Errors: Compiling playground v0.0.1 (/playground)
warning: enum is never used: `Foo`
--> src/lib.rs:1:6
|
1 | enum Foo {}
| ^^^
|
= note: `#[warn(dead_code)]` on by default None of the results of rust/src/librustc_passes/dead.rs Lines 1 to 3 in 31add7e
rust/src/librustc_passes/dead.rs Line 561 in 31add7e
Presumably the current implementation of this warning unconditionally considers code in trait impls to be reachable. I assume that privacy is also involved: a "normal" function is dead code if it’s private and never called, but if it’s public and never called in this crate it could still be called by a downstream dependency so it has to be considered reachable. However trait impls do not have privacy of their own, so it’s not as easy to find out if there could be a caller in another crate. I believe the change needed here is, instead of always treating trait impls as "public", use the most restrictive privacy between that of the trait and that of the type. This can be tricky when the impl is generic and may require to be conservative in some cases, but I believe it’s still possible to do better than the status quo. |
I think that this specifically comes from when struct Foo;
// Does not prevent the warning
trait Bar0 {}
impl Bar0 for Foo {}
// Does not prevent the warning
trait Bar1 { fn dummy(); }
impl Bar1 for Foo { fn dummy () {} }
// Prevents the warning
trait Bar2 { fn dummy(&self); }
impl Bar2 for Foo { fn dummy (&self) {} }
// Prevents the warning
trait Bar3 { fn dummy() -> Self; }
impl Bar3 for Foo { fn dummy () -> Self { todo!() } }
// Prevents the warning
trait Bar4 { type Dummy; }
impl Bar4 for Foo { type Dummy = Self; }
// Prevents the warning
trait Bar5 { const DUMMY: Self; }
impl Bar5 for Foo { const DUMMY: Self = Self; } |
Any updates here? |
Would it be reasonable to add a special signal to (I just hit this bug today, and it briefly gave me worries of “is my file not being compiled? I should get a warning”. Extra keywords for search: “struct is never constructed”.) |
For the record, I'd asked a related question on SO, and the answer has pointed out that the issue apparently got worse over time, because in the meantime all cases in @shepmaster's example prevent the warning. |
…elix Detect unused struct impls pub trait Fixes rust-lang#47851
Detect unused struct impls pub trait Fixes rust-lang#47851
This code emits no warnings because
impl Clone for Foo
is considered a "use" of the enum, however, implementation for this enum cannot possibly be used either, as the enum is private and is unused. This is more notable forderive
d implementations, which also inhibit this lint.The inherent implementations behave as expected.
The text was updated successfully, but these errors were encountered: