est31 / warnalyzer

Show unused code from multi-crate Rust projects
Other
89 stars 4 forks source link

Trait implementation methods not considered as used #3

Closed bjorn3 closed 5 years ago

bjorn3 commented 5 years ago
trait Abc {
    fn a();
    fn b();
}

struct Bcd;

impl Abc for Bcd {
    fn a() {}
    fn b() {} // considered unused even though it is required
}

fn main() {
    Bcd::a();
}
est31 commented 5 years ago

If the trait's function is being used anywhere even if it's through the impl of another struct, the warning disappears. This snippet is warning-free:

trait Abc {
    fn a();
    fn b();
}

struct Bcd;

impl Abc for Bcd {
    fn a() {}
    fn b() {}
}

struct Def;

impl Abc for Def {
    fn a() {}
    fn b() {}
}

fn main() {
    Bcd::a();
}

This is done by this line in the code. The reasoning is that if Abc::b() isn't used at all, then <Bcd as Abc>::b() isn't either. That's at least the basic idea. In general warnalyzer doesn't do recursion (doesn't warn in general if some code is only used by unused code) but this instance is some kind of manual mini-recursion. I didn't feel like it's worth implementing recursion now due to the high false positive rate.

Please tell me whether my explanation was helpful and whether this choice is okay in your opinion.

bjorn3 commented 5 years ago

I understand. It may be useful to ignore it for trait implementations, but keep it for trait definition, so that you only get one warning that the method is unused.