rust-lang / rust-clippy

A bunch of lints to catch common mistakes and improve your Rust code. Book: https://doc.rust-lang.org/clippy/
https://rust-lang.github.io/rust-clippy/
Other
11.26k stars 1.52k forks source link

How to generate "correct" paths for types in replacement snippets #3728

Open illicitonion opened 5 years ago

illicitonion commented 5 years ago

I'm writing a new lint (and have written one before, too), where the suggested replacement code needs to reference a type where it's non-obvious how the type has been imported into scope.

Is there a handy helper somewhere for "Given this type, give me the best way this code could reference it"? i.e. if the type is imported into scope, use its bare name, or if the module which defines it is in scope, reference it as module::Type, or if not, use ::fully::qualified::Type?

If not, are there any pointers for how I might go about implementing such a thing?

Thanks!

oli-obk commented 5 years ago

Rustc has some code around this for making the suggestion to add a use import if the type hasn't been imported yet. Unfortunately it is heavily entangled with the resolve code in the compiler. I'm not sure what kind of refactorings would be necessary in order to just query a few specific paths