rust-lang / rust-lang/rustfmt

Feature Request: Add spaces_within_parenthesized_items Option

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

Nobody has claimed this yet.

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

Description

Discussion for spaces_within_parenthesized_items configuration option. Implemented by #5434.

This option enforces consistent spacing directly inside of parentheses, by putting one space to the right of '(' and to the left of ')' in parenthesized items, such as function calls:

fn ipsum_lorem(arg1: usize, arg2: usize, arg3: usize)
to
fn ipsum_lorem( arg1: usize, arg2: usize, arg3: usize )

I do believe there are cases where having these spaces do improve readability, for example on calls:

ipsum_lorem<T>(dolor_amet<T>(string));
to
ipsum_lorem<T>( dolor_amet<T>( string ) );

Although I do understand that at the end of the day it is very subjective.

This option goes against some behaviours of the Rust Style Guide:
Single line calls and tuple literals.

Discussion topics:

  • Should tuple literals and struct tuple literals follow this rule as well?
(a, b, c)
Foo(a, b)

to

( a, b, c )
Foo( a, b )
  • What if there's comments and there's no parenthesized item, should spaces be added?
foo(/* comment */);

to

foo( /* comment */ );
  • What about attributes?
#[cfg(test)]

to

#[cfg( test )]
  • Patterns and grouping parentheses ?
(1 * ((2 + 3) * 4)

to

( 1 * ( ( 2 + 3 ) * 4 ) ) // I think grouping parentheses should not be included.
  • Is there a possibility of having the same rule for other symbols such as <> and []? You could write an array like this [ 1, 2, 3, 4, 5 ] and vec![ 1, 2, 3, 4 ].

Cheers!

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 discussion in this issue and the implementation referenced in #5434. Review the linked Rust Style Guide sections on single-line calls and tuple literals, then resolve the listed questions about tuples, comments, attributes, grouping parentheses, and other delimiters. Done means the option's scope and behavior are agreed and documented or implemented consistently.

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
Needs clarification
Newbie friendliness
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.