rust-lang / rust-lang/rust-clippy

Lint against non-idiomatic use paths

Open
#8,038 0 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-lint
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

What it does

Warn users about importing item other than modules, types, traits and derive macros.

Categories (optional)
  • Kind: clippy::style.

What is the advantage of the recommended code over the original code

From https://doc.rust-lang.org/book/ch07-04-bringing-paths-into-scope-with-the-use-keyword.html#creating-idiomatic-use-paths:

Specifying the parent module when calling the function makes it clear that the function isn’t locally defined while still minimizing repetition of the full path.

when bringing in structs, enums, and other items with use, it’s idiomatic to specify the full path.

The book does not say about how to import macros. Personally, I suggest that warn about importing:

  • functions,
  • function-like and attribute-like macros,
  • consts,
  • statics,

while allowing importing:

  • derive macros,
  • modules,
  • traits,
  • types.
Drawbacks

This style might not be suitable for everyone, and should not be enforced by default.

Example
mod front_of_house {
    pub mod hosting {
        pub fn add_to_waitlist() {}
    }
}

use self::front_of_house::hosting::add_to_waitlist;

pub fn eat_at_restaurant() {
    add_to_waitlist();
    add_to_waitlist();
    add_to_waitlist();
}

Could be written as:

mod front_of_house {
    pub mod hosting {
        pub fn add_to_waitlist() {}
    }
}

use self::front_of_house::hosting;

pub fn eat_at_restaurant() {
    hosting::add_to_waitlist();
    hosting::add_to_waitlist();
    hosting::add_to_waitlist();
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start from the issue's examples and the linked Rust Book section on creating idiomatic use paths. Define the lint behavior for functions, function-like and attribute-like macros, consts, and statics, while allowing derive macros, modules, traits, and types. Done means the proposed non-default style lint consistently distinguishes these item categories.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.