rust-lang / rust-lang/rustfmt

Keep `use`d items on individual lines when requested

Open
#6,829 7 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Feature Request

Summary

This is a request for an option to not collapse items in braced blocks into a single line. E.g.:

This (Example 1):

use crate::{
    example1,
};

is now collapsed to:

use crate::example1;

And this (Example 2):

use crate::{
    example1,
    example2::{
        example3,
        example4,
        example5,
    },
    example6,
    example7,
    example8::example9,
};

is collapsed to:

use crate::{
    example1,
    example2::{example3, example4, example5},
    example6, example7,
    example8::example9,
};

We would like for these items to remain within the braced blocks.

Motivation

This comes form the Linux kernel, where the coding style guidelines request each imported item be on a separate line:

https://docs.kernel.org/rust/coding-guidelines.html#imports

The justification is to minimise the diff when adding/removing new imports. This makes the changes easier to review as well as comment on.

That same reasoning can be extended to anyone who'd like to have the "line churn" reduced when making changes to the use sections.

Workaround

Right now, the Rust code in Linux works around this by appending a trailing double slash (//, an empty comment) to the last item before the ending brace, which leaves the spacing as is:

use crate::{
    example1, //
};

and:

use crate::{
    example1,
    example2::{
        example3,
        example4,
        example5, //
    },
    example6,
    example7,
    example8::example9, //
};

In other words, we'd like to have this behaviour without having to specify the empty comments.

Related configuration options

The Vertical imports_layout is almost what we want (it covers Example 2), but it collapses a single item within braces (Example 1). We'd like to be able to do both.

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 reading the existing imports_layout documentation and the discussion in issue #3361. Compare the current Vertical behavior with the two examples in this issue, then determine how the requested option should preserve braced use items, including single-item blocks, without requiring trailing comments.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
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.