Lint suggestion: require `self` for relative `use` statement paths

Open
#14,246 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
48/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Quiet
Tech stack
rust
Domain
tooling

Research direction

No implementation file, test, or entry point is named. Start by reviewing existing Clippy lint implementations and how they inspect use statements, then compare the requested self-prefixed behavior with the examples. Done means the proposed lint behavior is covered by tests and is not enabled by default.

Written by the indexing model from the issue text.

Description

A-lint
What it does

Require self before paths in use statements that are relative to self.

Advantage

I found out that I'd want to easily distinguish between imports of my code and imports from other crates, and currently I can't easily distinguish between them.

A possible solution is to require adding self before relative imports. This makes it easy to distinguish between the two kinds, since local imports will always start with self, crate or super.

This would also make it easier to organize the imports according to their kind, like what the unstable rust-fmt option group_imports = StdExternalCrate does.

I don't suggest to enable this lint by default.

Drawbacks

I don't think of drawbacks, but I won't be surpried if others would think of some...

Example
use my_module::my_function;

Could be written as:

use self::my_module::my_function;
Note

If this is approved, I'd be happy to try to implement this.

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

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.

More from rust-lang/rust-clippy

All issues in rust-lang/rust-clippy

Similar issues

More Rust issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.