Is there any sane way to get a collection of ModuleDef::Trait that correspond to a given hir Adt?
Or maybe a way to get a collection of ModuleDef::Trait that have a hir Function defined in them?

In other words, I want to either find all traits that are implemented for a given Adt or find all traits that have the particular function definition.

One strong limitation that makes it fun: the trait is not in the current scope (i.e. no use statement for it from the place we perform a search hence no SourceAnalyzer::resolve_path can be used).

So far I've found an eerie way to get that as

ImplBlock::all_in_crate
-> filter out the ones that target the Adt only
-> get the ra_hir_def::TypeRef via ImplBlock::target_trait on the remaining
-> scrape the trait name out of it (by considering the TypeRef::Path variants only)
-> do ImportsLocator::find_imports on the name

Can there be a better way?
It would be great to get the Trait instead of TypeRef somehow at least.

How do I do that?
I wonder why do I need to change this method, if I'm able to get all traits? :)

You're right, you don't need to change it since it takes the set of traits as a parameter nowadays

I think to get the set of candidate traits the best way would be to go through the CrateDefMap of the current crate and its direct dependencies, and collect the set of all traits in all modules in there. (We might want to make an index of that, but I guess it's better to actually first try the brute-force way)

method resolution filters by the function first anyway

but getting all traits with a function with a certain name via an index would of course be nicer

If there's a way to get to get impls for a given Trait or TraitDef, I can avoid using the method_resolution method at all.
I think I should, because otherwise I need to soemhow make it available in ra_assists crate which does not make much sense to me.

The callback signature is mut callback: impl FnMut(&Ty, AssocItem) -> Option<T>, so it does not return a trait.
Or is the plan to call the method for each trait I've located before, one by one and see if something gets passed into the callback for that trait?

for a Function as well you can get the container. you might need to add methods for it in code_model, but it's straightforward

Ah, silly me, got it. Thanks.

And do I get it right, that ImplBlock::for_trait approach is worse than the way you've proposed because it does not consider derefs?

derefs aren't relevant for path-style method calls, but to decide whether a type implements a trait you have to use chalk, you can't just look at the impls. consider impls like impl<T> Trait for T where T: SomeOtherTrait