rust-lang / rust-lang/rustfmt

Import grouping order is inconsistent with import sorting order

Open
#6,819 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-discussion UO-group_imports
Dominant language
Rust
Stars
7k
Forks
1.1k
Avg merge
2d 13h
Merged PRs (30d)
24

Description

With group_imports set to StdExternalCrate, Rustfmt will convert

use crate::a;
use super::b;
use self::c;
use itertools::d;
use std::e;

to

use std::e;

use itertools::d;

use self::c;
use super::b;
use crate::a;

The import groups are ordered from most general to most local, but self, super and crate are sorted in the other direction.

The import grouping order makes most sense to me. I'd suggest that when group_imports is StdExternalCrate, the order of crate, super and self should be reversed, giving a nice general → local order, thus:

use std::e;

use itertools::d;

use crate::a;
use super::b;
use self::c;

Or perhaps crate, super and self ought to get their own groups, though perhaps not, as they're less common than std and external/workspace imports in my experience. (Our code-base is 25 % std, 35 % external, 15 % workspace, 10 % crate, 10 % super and 5 % self, including tests but excluding documentation examples.)

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

No source file or test is named. Start by locating rustfmt's import grouping and sorting implementation, then compare the existing StdExternalCrate behavior with the ordering shown in the issue; done means a regression test and formatter output that consistently order std, external, crate, super, and self from general to local.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.