rust-lang / rust-lang/rustfmt

Imports are not formatted when long import lines are present

Open
#4,746 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-imports E-help-wanted I-poor-formatting P-low
Dominant language
Rust
Stars
7k
Forks
1.1k
Avg merge
2d 13h
Merged PRs (30d)
24

Description

On some inputs containing long lines, rustfmt makes no changes, either to those lines or any other imports. Using shorter identifiers fixes the problem.

To Reproduce

Run rustup run nightly rustfmt --verbose --config imports_granularity=Crate --emit stdout on this input file:

use foo::bar;
use foo::{quuz, baz};
use abaadfsasdfdsfdfas::aasdffjsioejr::abc::sdsdf::sdfsdfsdf::sdfsdfdsf::{
    asdfasdefasdasdfsdfdfasdf::asdfasdasedfafasdfasdf,
};

The input file will be returned unchanged.

Expected behavior

Delete the final s from the initial abaadfsasdfdsfdfas and the correct behavior is seen; the output is

use abaadfsasdfdsfdfa::aasdffjsioejr::abc::sdsdf::sdfsdfsdf::sdfsdfdsf::asdfasdefasdasdfsdfdfasdf::asdfasdasedfafasdfasdf;
use foo::{bar, baz, quuz};

I can reproduce the same behavior with merge_imports=true on stable.

Meta

  • rustfmt version: rustup run nightly rustfmt --version returns rustfmt 1.4.36-nightly (7de6968 2021-02-07).
  • From where did you install rustfmt?: rustup
  • How do you run rustfmt: rustfmt

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 running the reported rustup nightly rustfmt command with imports_granularity=Crate on the supplied input, then repeat with merge_imports=true on stable. Compare the unchanged output with the expected merged and sorted imports, and consider the issue resolved when both configurations format the long import and the other imports as shown.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.