rust-lang / rust-lang/rustfmt

Feature request: Option to have one "use" statement per file

Open
#5,360 9 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-imports C-feature-request P-low
Dominant language
Rust
Stars
7k
Forks
1.1k
Avg merge
2d 13h
Merged PRs (30d)
24

Description

Currently imports_granularity = 'One' merges all imports into one "use" statement. In my opinion, functionally it does two separate things: 1) Combine all "use" statements; 2) apply imports_granularity = 'Crate' inside the braces. Obviously it has the advantage of having no duplicate "use". However, I also really like the style of imports_granularity = 'Module'. It's visually much clearer, and easier to modify one import without affecting other imports. Ideally, I'd love to see a style where it can combine the "one" use statement with the module style. For example,

use {
    foo::b::{f, g},
    foo::d::e,
    foo::{a, b, c},
    qux::{h, i},
};

I think imports_granularity is orthogonal to this one "use" statement style, so why not introduce a separate option like one_use_statement? With this new option, we will have 6 combinations in total:

  1. imports_granularity = 'Crate' + one_use_statement = false

  2. imports_granularity = 'Module' + one_use_statement = false

  3. imports_granularity = 'Item' + one_use_statement = false
    These 3 will be exactly the same as current

  4. imports_granularity = 'Crate' + one_use_statement = true
    This is basically identical to the current imports_granularity = 'One'

  5. imports_granularity = 'Module' + one_use_statement = true
    The example is shown above

  6. imports_granularity = 'Item' + one_use_statement = true

use {
    foo::a,
    foo::b,
    foo::b::f,
    foo::b::g,
    foo::c,
    foo::d::e,
    qux::h,
    qux::i,
};

With this new option, imports_granularity = 'One' is no longer needed.

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

The issue does not name files, tests, or an entry point. Start by locating rustfmt's existing imports_granularity configuration and related import-formatting tests, then determine how a separate one-use-statement option should interact with the existing modes and update coverage for the proposed combinations.

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.