rust-lang / rust-lang/rust-clippy

Lint against inconsistent import paths

Open
#12,878 1 comment 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

For example, there are some import paths A, a::A, ab::A and ac::A to the object A, where A, a::A are visible to mod b and mod c, ab::A is visible to mod b, ac::A is visible to mod c.
So there are multiple choices for mod b and mod c to import A

mod b mod c
1 A a::A
2 A ac::A
3 ab::A ac::A

A weaker version of this lint would warn if the import paths to the same object in the project is inconsistent and can be unified (in the first case).
An stronger version of this lint would warn if the import path to the object is not following a predefined rule (in all 3 cases), the rule can be

  • use the shortest absolute path
  • use the path which has the least restrictive visibility

It's not a mature idea but I think something like this is helpful.

Advantage
  • the import paths are more stable against editing, producing less git diff noises
  • make it easier to refactor
Drawbacks
  • it's nonlocal. So Maybe it's hard to implement or it takes a lot computing resources to run.
Example
// lib.rs
pub use a::A;

pub(crate) mod a;
pub(crate) mod b;
pub(crate) mod c;

// a.rs
pub struct A;

// b.rs
use crate::A;

struct B(A);

// c.rs
use crate::a::A;

struct C(A);

Could be written as:

// lib.rs
pub use a::A;

pub(crate) mod a;
pub(crate) mod b;
pub(crate) mod c;

// a.rs
pub struct A;

// b.rs
use crate::A;

struct B(A);

// c.rs
use crate::A;

struct C(A);

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 with the issue's lib.rs, a.rs, b.rs, and c.rs examples to understand the proposed import-path equivalence and visibility rules. No implementation files or tests are named; define the lint's scope and rule first, then determine how it would analyze nonlocal imports and add coverage demonstrating the intended warnings.

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
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.