rust-lang / rust-lang/rustfmt

Add `blankline_before_or_after_brace` option.

Open
#5,173 10 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Environment: rustfmt 1.4.37-stable (f1edd042 2021-11-29) (with no configuration file)

Currently, blank lines before the opening brace { or after the closing brace } are removed.

fn main() {

    println!("Hello, world!");

}

fn main() {
    println!("Hello, world!");
}

This style is hard to read especially when the signature (plus where clause) and the body are long:

use regex::Regex;
use std::process::Command;
use std::process::Output;

fn _some_function<T, U, V, W>(
    _some_param_1: T,
    _some_param_2: &U,
    _some_param_3: Vec<V>,
    _some_param_4: &[W],
    _some_param_5: String,
) -> String
where
    T: Iterator,
    T::Item: Clone,
    U: std::fmt::Display + Sync,
    V: std::hash::Hash,
    W: PartialEq + PartialOrd,
{
    let regex = Regex::new(r"\$\((.*)\)").unwrap(); // <--- I want a blank line above this line!

    let mut s = String::new();

    match regex.captures(&s) {
        Some(c) => {
            let command = c.get(1).unwrap().as_str();
            let range = c.get(0).unwrap().range();
            let res: Output = Command::new("bash").args(["-c", command]).output().unwrap();
            let res: String = String::from_utf8(res.stdout).unwrap().trim().to_string();
            s.replace_range(range, &res);
        }
        None => (),
    }

    let a = 5;
    println!("{}", a);

    std::env::args().for_each(|e| println!("{}", e));

    String::from("hello")
}

I've checked all the stable and unstable configuration options listed in Configuring Rustfmt, but it seemed there was no option to preserve blank lines before or after braces.

So this is a feature request:

blankline_before_or_after_brace

Default value: false

Possible values: false, true, preserve

Behavior:

  • false (default): No blank line before or after braces is allowed.

  • true: Always insert a blank line before or after each brace.

  • preserve: Preserve the style in the original code.

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 with the rustfmt configuration documentation linked in the issue and inspect the existing behavior that removes blank lines around braces. Determine how the proposed false, true, and preserve values should behave for the examples shown, then verify the resulting formatting against those examples.

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.