rust-lang / rust-lang/rustfmt

[Feature-Request] Blank-lines surrounding curly braces

Open
#5,963 7 comments 3 reactions 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

Sorry if this is a dup, I searched for a little bit and only found issues related to blank lines inside curly braces.

I have no idea how hard this would be, but it would be really nice to have a configurable option to add blank lines surrounding anything with curly braces.

with the rule on

fn main() {
  let some_optional_value = map.get(&key); 
  
  if let Some(value) = some_optional_value {
      do_something_with_value(value);
  }
}

with the rule off

fn main() {
  let some_optional_value = map.get(&key);  
  if let Some(value) = some_optional_value {
      do_something_with_value(value);
  }
}

This would include for-loop, loop, while, etc. Any expressions with a scope defined by curly braces.

I'm thinking something akin to this rule: https://eslint.org/docs/latest/rules/padding-line-between-statements

I'm trying to have some automatic way to not get code that looks like this:

        let mut items = HashMap::new();
        for _ in 0..100 {
            let val: Vec<u8> = (0..10).map(|_| rng.borrow_mut().gen()).collect();
            items.insert(generate_key(), val);
        }
        let mut items_ordered: Vec<_> = items.clone();
        items_ordered.sort();
        items_ordered.shuffle(&mut *rng.borrow_mut());
        for (k, v) in items.into_iter() {
            other_map.insert(&k, v)?;
        }

and have the formatted add some empty lines to make the code easier to read

        let mut items = HashMap::new();

        for _ in 0..100 {
            let val: Vec<u8> = (0..10).map(|_| rng.borrow_mut().gen()).collect();
            items.insert(generate_key(), val);
        }

        let mut items_ordered: Vec<_> = items.clone();
        items_ordered.sort();
        items_ordered.shuffle(&mut *rng.borrow_mut());

        for (k, v) in items.into_iter() {
            other_map.insert(&k, v)?;
        }

Obviously, not everyone wants this, but it would be great if it were an option. I'm happy to PR, if someone can point me in the right direction.

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 files, tests, or entry points are named. Start by reviewing the requested Rust examples and determining where formatting options and curly-brace scopes are handled. Done means a configurable rule adds surrounding blank lines for the described scoped constructs while leaving formatting unchanged when disabled, with coverage for the 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.