Description
What it does
We get a lot of contributions that, for one reason or another, reference symbols with absolute paths instead of having a use
declaration at the top. E.g.,
fn main() {
println!("{}", std::cmp::max(1, 2));
}
instead of
use std::cmp::max;
fn main() {
println!("{}", max(1, 2));
}
It's not wrong, of course, and variations are possible. E.g., some projects prefer use std::cmp;
over use std::cmp::max
etc. pp.
But: I am not aware of a single project that uses absolute style. As such, I think there is value in flagging it (without necessarily suggesting an alternative, given that there is more than one). I would certainly find it useful, as it's cumbersome to point out those cases during review, as opposed to having a machine provide assurance that there are none.
Lint Name
absolute-symbol-path
or perhaps symbol-reference-without-use
Category
style, pedantic
Advantage
Point out inconsistent use
style. For better or worse, most code bases and guides have explicit use
statements that import relevant symbols, so that they can be used without having to provide an absolute path/full reference to the symbol at each call/use site. Having some symbols use absolute paths in the middle of code while others have explicit imports seems l;ike an inconsistency that, for no good reason, and could give a false impression of what functionality is used by a module when just checking use
statements.
Drawbacks
I don't believe there are drawbacks, but it's a subjective matter, so there are differing opinions on what folks prefer. Should probably be disabled by default.
That being said, macros should probably be excluded from this lint, as it absolutely makes sense for macros to use absolute imports (and it's even the right thing to do in most cases, but that's a different story).
Example
fn main() {
println!("{}", std::cmp::max(1, 2));
}
Could be written as:
use std::cmp::max;
fn main() {
println!("{}", max(1, 2));
}