Disallow item usage by configurable paths (possibly created by extern crate self as xxx;
)
#14706
Labels
A-lint
Area: New lints
What it does
There's a relatively common pattern in libraries that internally use their own procedural macros, which are also meant to be used externally. If these macros refer to any items from the public library, they need to do this via its public name, say
xxx
. However, if thexxx
crate now uses these macros internally, they fail to build becausexxx
crate is not defined; you can only refer to it bycrate
. The easy solution is to add the following line tolib.rs
:(see rust-lang/rust#56409)
But this creates a new problem: now you can refer to items in your
xxx
crate either bycrate::foo::bar
, orxxx::foo::bar
. This creates inconsistency in the code style, especially when using something likegroup_imports = "StdExternalCrate"
in yourrustfmt
config (which causescrate
imports to be grouped separately from the external crates).The idea is to create a lint that will disallow using
xxx
in paths ifcrate
can be used instead and will lead to using the same type.A possible extension that could improve the usefulness of the lint would be to make it configurable, similarly to
missing_enforced_import_renames
. For example, it might be undesirable to importsome_crate_with_reexport::rand::Rng
instead ofrand::Rng
, even if it would result in using the same type. This could be configurable like so:Advantage
Drawbacks
None that I can think of.
Example
Could be written as:
If this lint is something that could be useful, I'm happy to implement it.
The text was updated successfully, but these errors were encountered: