Skip to content

Add option space_before_fn_sig_paren #4302

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jul 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions Configurations.md
Original file line number Diff line number Diff line change
Expand Up @@ -2095,6 +2095,52 @@ fn lorem<T : Eq>(t : T) {

See also: [`space_after_colon`](#space_after_colon).

## `space_before_fn_sig_paren`

Whether to put a space before the opening paren in function signatures

- **Default value**: `false`
- **Possible values**: `true`, `false`
- **Stable**: No

#### `false` (default):

```rust
fn lorem() {
// body
}

fn lorem(ipsum: usize) {
// body
}

fn lorem<T>(ipsum: T)
where
T: Add + Sub + Mul + Div,
{
// body
}
```

#### `true`:

```rust
fn lorem () {
// body
}

fn lorem (ipsum: usize) {
// body
}

fn lorem<T> (ipsum: T)
where
T: Add + Sub + Mul + Div,
{
// body
}
```

## `spaces_around_ranges`

Put spaces around the .., ..=, and ... range operators
Expand Down
3 changes: 3 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ create_config! {
spaces_around_ranges: bool, false, false, "Put spaces around the .. and ..= range operators";
binop_separator: SeparatorPlace, SeparatorPlace::Front, true,
"Where to put a binary operator when a binary expression goes multiline";
space_before_fn_sig_paren: bool, false, false,
"Whether to put a space before the opening paren in function signatures";

// Misc.
remove_nested_parens: bool, true, true, "Remove nested parens";
Expand Down Expand Up @@ -597,6 +599,7 @@ space_after_colon = true
space_around_attr_eq = true
spaces_around_ranges = false
binop_separator = "Front"
space_before_fn_sig_paren = false
remove_nested_parens = true
combine_control_expr = true
overflow_delimited_expr = false
Expand Down
4 changes: 4 additions & 0 deletions src/formatting/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2228,6 +2228,10 @@ fn rewrite_fn_base(
.last()
.map_or(false, |l| l.trim_start().len() == 1);

if context.config.space_before_fn_sig_paren() {
result.push(' ');
}

// Note that the width and indent don't really matter, we'll re-layout the
// return type later anyway.
let ret_str = fd
Expand Down
19 changes: 19 additions & 0 deletions tests/source/configs/space_before_fn_sig_paren/example.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// rustfmt-space_before_fn_sig_paren: true
// rustfmt-max_width: 30
// Function space before function paren

fn foo() {
// ...
}
fn foo_with_multi_lined(a: u32, b: u32, c: u32) {
// ...
}
fn foo<T>(bar: T) {
// ...
}
fn foo<T>(a: T, b: u32, c: u32) {
// ...
}
fn foo<T: Foo + Bar, F: FooBar>(a: T, b: u32, c: u32) {
// ...
}
17 changes: 17 additions & 0 deletions tests/source/configs/space_before_fn_sig_paren/fn.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// rustfmt-space_before_fn_sig_paren: true
// Function space before function paren

fn lorem() {
// body
}

fn lorem(ipsum: usize) {
// body
}

fn lorem<T>(ipsum: T)
where
T: Add + Sub + Mul + Div,
{
// body
}
20 changes: 20 additions & 0 deletions tests/source/configs/space_before_fn_sig_paren/indent_style.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// rustfmt-space_before_fn_sig_paren: true
// rustfmt-indent_style: Visual
// rustfmt-max_width: 30
// Function space before function paren

fn foo() {
// ...
}
fn foo_with_multi_lined(a: u32, b: u32, c: u32) {
// ...
}
fn foo<T>(bar: T) {
// ...
}
fn foo<T>(a: T, b: u32, c: u32) {
// ...
}
fn foo<T: Foo + Bar, F: FooBar>(a: T, b: u32, c: u32) {
// ...
}
13 changes: 13 additions & 0 deletions tests/source/configs/space_before_fn_sig_paren/max_width.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// rustfmt-space_before_fn_sig_paren: true
// rustfmt-max_width: 118
// Trait space before function paren

trait Story {
fn swap_context<T: 'static + Context + Send + Sync>(&mut self, context: T) -> Option<Box<Context + Send + Sync>>;
}

impl Story for () {
fn swap_context<T: 'static + Context + Send + Sync>(&mut self, context: T) -> Option<Box<Context + Send + Sync>> {
// ...
}
}
17 changes: 17 additions & 0 deletions tests/source/configs/space_before_fn_sig_paren/trait.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// rustfmt-space_before_fn_sig_paren: true
// Trait space before function paren

trait Story {
fn swap_context<T>(&mut self, context: T) -> Option<Box<Context>>
where
T: Context;
}

impl Story {
fn swap_context<T>(&mut self, context: T) -> Option<Box<Context>>
where
T: Context,
{
// ...
}
}
34 changes: 34 additions & 0 deletions tests/target/configs/space_before_fn_sig_paren/example.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// rustfmt-space_before_fn_sig_paren: true
// rustfmt-max_width: 30
// Function space before function paren

fn foo () {
// ...
}
fn foo_with_multi_lined (
a: u32,
b: u32,
c: u32,
) {
// ...
}
fn foo<T> (bar: T) {
// ...
}
fn foo<T> (
a: T,
b: u32,
c: u32,
) {
// ...
}
fn foo<
T: Foo + Bar,
F: FooBar,
> (
a: T,
b: u32,
c: u32,
) {
// ...
}
17 changes: 17 additions & 0 deletions tests/target/configs/space_before_fn_sig_paren/fn.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// rustfmt-space_before_fn_sig_paren: true
// Function space before function paren

fn lorem () {
// body
}

fn lorem (ipsum: usize) {
// body
}

fn lorem<T> (ipsum: T)
where
T: Add + Sub + Mul + Div,
{
// body
}
29 changes: 29 additions & 0 deletions tests/target/configs/space_before_fn_sig_paren/indent_style.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// rustfmt-space_before_fn_sig_paren: true
// rustfmt-indent_style: Visual
// rustfmt-max_width: 30
// Function space before function paren

fn foo () {
// ...
}
fn foo_with_multi_lined (a: u32,
b: u32,
c: u32)
{
// ...
}
fn foo<T> (bar: T) {
// ...
}
fn foo<T> (a: T,
b: u32,
c: u32) {
// ...
}
fn foo<T: Foo + Bar,
F: FooBar> (
a: T,
b: u32,
c: u32) {
// ...
}
17 changes: 17 additions & 0 deletions tests/target/configs/space_before_fn_sig_paren/max_width.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// rustfmt-space_before_fn_sig_paren: true
// rustfmt-max_width: 118
// Trait space before function paren

trait Story {
fn swap_context<T: 'static + Context + Send + Sync> (&mut self, context: T)
-> Option<Box<Context + Send + Sync>>;
}

impl Story for () {
fn swap_context<T: 'static + Context + Send + Sync> (
&mut self,
context: T,
) -> Option<Box<Context + Send + Sync>> {
// ...
}
}
17 changes: 17 additions & 0 deletions tests/target/configs/space_before_fn_sig_paren/trait.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// rustfmt-space_before_fn_sig_paren: true
// Trait space before function paren

trait Story {
fn swap_context<T> (&mut self, context: T) -> Option<Box<Context>>
where
T: Context;
}

impl Story {
fn swap_context<T> (&mut self, context: T) -> Option<Box<Context>>
where
T: Context,
{
// ...
}
}