Skip to content

Unname method-only trait imports #9021

Closed
@SOF3

Description

@SOF3

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")
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintArea: New lints

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions