Detect pub structs never constructed and unused associated constants by mu001999 · Pull Request #125572 · rust-lang/rust (original) (raw)
I found the state of this analysis interesting, especially the fact that it flags the struct as dead even when there's a impl Clone
of it sitting there.
This makes sense: the presence of clone does not provide evidence that the struct is live, because you need an instance of the struct in order to be able to enter the clone code in the first place, in order to call the fn clone(&self) -> Self
method.
But it led me to wonder: How far does this reasoning go? I.e., does it also filter out other variations of the same inductive reasoning, such as a variant trait that takes x: Self
or x: &Self
?
So I built a local copy, and soon determined: No, the reasoning does not go that far. That is, either of the following trait impls will suffice to treat a struct constructor as "live code":
pub trait U { fn f(_: Self) -> Self; } impl U for S { fn f(x: Self) -> Self { S(10) } }
pub trait W { fn f(_: &Self) -> Self; } impl W for S { fn f(x: &Self) -> Self { S(10) } }
Note: This is fine. I'd rather we start with a lint that doesn't attempt to catch such cases, than risk shipping an over-aggressive lint that ends up also accidentally treating things like fn f(x: Option<&Self>) -> Self
as proof of life.