rust-lang / rust-lang/rust-clippy

Lint against `super` (and `self`) imports

Open
#12,796 2 comments 11 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 a lot of crates, it can be convenient and easier to read global crate-based imports (i.e. use crate::a::b::c instead of super::c), and IDEs can often automatically put in super-style imports, which can then be confusing when also mixed with absolute (crate::) imports. It'd be really nice to have a lint that could warn against super:: style imports, or at least lint against inconsistency between the two (i.e. using both crate:: and super:: imports in the same file), or at least for using super- and crate- style imports to import different things from the same file. For example, in this module structure:

src/main.rs (defines a static FOO and a static BAR)
src/directory/mod.rs (needs to import FOO and BAR)

src/directory/mod.rs can do use crate::FOO and use super::BAR, and it's totally unclear that these are coming from the same place, which can be confusing.

The same goes for self:: imports; it'd be really convenient to at least be able to warn when different imports import things from the same file, if not warn against import styles in general (I, for example, prefer to only use absolute crate:: imports in all my files, so it'd be really handy to be able to lint against super:: and self:: imports entirely, though I can understand the argument for this being a bit too restrictive).

Advantage

The new code unifies the imports into a single unified style and makes it clear when imports are coming from the same location. Similarly, "close together" files are often close together in the imports, which isn't necessarily true with a split import style. Also, the imports are often more concise, i.e., doing use crate::{FOO, BAR} is less characters than use crate::FOO; use super::BAR;. It also would help for situations where IDEs auto-import in a style against what a project is using, which can quickly create messy imports in files.

Drawbacks

Some people may prefer to mix import styles, especially for test modules and whatnot (though they could just not enable the lint of course, but obviously that applies to every lint). Some people may have some specific system or preference that involves mixing crate::, super:: and self:: imports.

Example

In a file structure as follows:

src/main.rs (defines a static FOO and a static BAR)
src/directory/mod.rs (needs to import FOO and BAR)

This code in src/directory/mod.rs:

use crate::FOO;
use super::BAR;

Could be written as:

use crate::{FOO, BAR};

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 by reviewing the proposed import patterns in src/main.rs and src/directory/mod.rs, including the crate::, super::, and self:: cases. Decide whether the lint should reject styles outright or detect mixed imports from the same location; done means the chosen behavior is clearly specified and covered for the demonstrated examples.

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.