How to find comparisons of a given type?

I have a giant Rust codebase (one workspace containing a bunch of crates) that contains something like this:

pub struct Foo { .. }

impl PartialOrd for Foo {
    fn partial_cmp((&self, other: &Self) -> Option<Ordering> { .. }
}
// also `Eq` and `PartialEq` are implemented consistently with `PartialOrd`

Is there a way I can find all the places in my codebase where I'm comparing two instances of Foo using my PartialOrd implementation?

I've tried using rust-analyzer's "go to reference" functionality, but it only finds explicit calls to the partial_cmp function, and not any of the comparison operators (like <). I've also tried adding explicit definitions of the comparison functions (e.g. overriding fn lt(..)) to "go to reference" on it, but it also misses the comparison operators, as it'll find x.lt(&y), but not x < y).

You can comment out these impls so that you get errors wherever you're using the comparison operators. This seems like something rust-analyzer should be able to do, though, so maybe open a feature request?

1 Like

Sadly, if one crate in the workspace fails to compile because of the missing comparison, then it can't try to build any crates that depend on that failing crate, so that's potentially a very long, manual process.

1 Like