Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unname method-only trait imports #9021

Closed
SOF3 opened this issue Jun 19, 2022 · 0 comments · Fixed by #13322
Closed

Unname method-only trait imports #9021

SOF3 opened this issue Jun 19, 2022 · 0 comments · Fixed by #13322
Labels
A-lint Area: New lints

Comments

@SOF3
Copy link

SOF3 commented Jun 19, 2022

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 with anyhow::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")
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant