Closed
Description
What it does
If a trait is imported only for its methods but not its name directly, append as _
to the import
Lint Name
unname_method_only_trait_imports
Category
style
Advantage
- The imported name isn't actually used. Keeping it would result in conflicts in the future when other items with the same identifier are imported, at which point developers have to corrupt the commit diff to modify it
- Some names are used at really high frequency, e.g.
anyhow::Context
. In fact, the last 5 times I ran into name collision, it was to do withanyhow::Context
.
Drawbacks
Editors do not support this right now, implying that we need a cargo fix
for this to be useful.
Example
mod universe {
use anyhow::{Context, Result};
fn foo() -> Result<()> {
fs::remove_file("file").context("unlink")
}
}
Could be written as:
mod universe {
use anyhow::{Context as _, Result};
fn foo() -> Result<()> {
fs::remove_file("file").context("unlink")
}
}