rust-lang / rust-lang/rustfmt

[Feature Request] Allow configurable number of blank lines before control structures

Open
#7,120 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Feature Request

Allow configurable number of blank lines before control structures like the following:

  • for
  • while
  • match
  • if/else
  • loop

Take a look at these examples:

Before:

fn main() {
    let data = vec![1, 2, 3];
    for d in data {
       // ....
    }
}

After:

fn main() {
    let data = vec![1, 2, 3];

    for d in data {
      // notice the blank line before this for loop
    }
}

However, it should not do it for the following (in the else branch):

let x = if condition {
    foo()
} else {
    bar()
};

and not for nested control structures:

if a {
    if b {
        ...
    }
}

Perhaps these can come as:

blank_lines_before_control_flow_statements = 1
Summary

Allow configurable number of blank lines before control flow structures.

Motivation

In cases where the number of lines is a lot, having no blank lines before control flow structures makes it very difficult to scan implementations. While it is trivial to add a line before control structures, I would like to have something automated across the codebase and this can be useful in cases where codegen (not necessarily LLMs, more like using tools like an openapi -> rust generator) is involved

This was discussed back in 2017 in https://github.com/rust-lang/style-team/issues/57 except that my particular feature request is a much more narrow impl of it. Apologies if I missed out other discussion surrounding this

Related configuration options

AFAIK, blank_lines_lower_bound and blank_lines_upper_bound are tangibly related but kinda different.

Possibly related issues
PR

I am open to try to implement this feature if the team and wider community is in a agreement with this.

Thanks!

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 rustfmt's existing blank_lines_lower_bound and blank_lines_upper_bound options, then read related issue #6854 and the style-team discussion in issue #57. The work is done when a configurable blank-line count applies to the listed top-level control structures while excluding else branches and nested control structures, with the behavior covered by appropriate tests.

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
Active
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.