Skip to content

Commit bcf09cb

Browse files
authored
Add option space_before_fn_sig_paren (#4302)
* Add option space_after_function_name * Rename option and change type to bool * Revert "Rename option and change type to bool" This reverts commit 6350a15. * Cover other styles * Fix comments from review * Use simpler interface * Add indent_style test * Rename to space_before_fn_sig_paren
1 parent ebf0de7 commit bcf09cb

File tree

13 files changed

+253
-0
lines changed

13 files changed

+253
-0
lines changed

Configurations.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2129,6 +2129,52 @@ fn lorem<T : Eq>(t : T) {
21292129

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

2132+
## `space_before_fn_sig_paren`
2133+
2134+
Whether to put a space before the opening paren in function signatures
2135+
2136+
- **Default value**: `false`
2137+
- **Possible values**: `true`, `false`
2138+
- **Stable**: No
2139+
2140+
#### `false` (default):
2141+
2142+
```rust
2143+
fn lorem() {
2144+
// body
2145+
}
2146+
2147+
fn lorem(ipsum: usize) {
2148+
// body
2149+
}
2150+
2151+
fn lorem<T>(ipsum: T)
2152+
where
2153+
T: Add + Sub + Mul + Div,
2154+
{
2155+
// body
2156+
}
2157+
```
2158+
2159+
#### `true`:
2160+
2161+
```rust
2162+
fn lorem () {
2163+
// body
2164+
}
2165+
2166+
fn lorem (ipsum: usize) {
2167+
// body
2168+
}
2169+
2170+
fn lorem<T> (ipsum: T)
2171+
where
2172+
T: Add + Sub + Mul + Div,
2173+
{
2174+
// body
2175+
}
2176+
```
2177+
21322178
## `spaces_around_ranges`
21332179

21342180
Put spaces around the .., ..=, and ... range operators

src/config.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ create_config! {
9494
spaces_around_ranges: bool, false, false, "Put spaces around the .. and ..= range operators";
9595
binop_separator: SeparatorPlace, SeparatorPlace::Front, true,
9696
"Where to put a binary operator when a binary expression goes multiline";
97+
space_before_fn_sig_paren: bool, false, false,
98+
"Whether to put a space before the opening paren in function signatures";
9799

98100
// Misc.
99101
remove_nested_parens: bool, true, true, "Remove nested parens";
@@ -600,6 +602,7 @@ space_after_colon = true
600602
space_around_attr_eq = true
601603
spaces_around_ranges = false
602604
binop_separator = "Front"
605+
space_before_fn_sig_paren = false
603606
remove_nested_parens = true
604607
combine_control_expr = true
605608
overflow_delimited_expr = false

src/formatting/items.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2255,6 +2255,10 @@ fn rewrite_fn_base(
22552255
.last()
22562256
.map_or(false, |l| l.trim_start().len() == 1);
22572257

2258+
if context.config.space_before_fn_sig_paren() {
2259+
result.push(' ');
2260+
}
2261+
22582262
// Note that the width and indent don't really matter, we'll re-layout the
22592263
// return type later anyway.
22602264
let ret_str = fd
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// rustfmt-space_before_fn_sig_paren: true
2+
// rustfmt-max_width: 30
3+
// Function space before function paren
4+
5+
fn foo() {
6+
// ...
7+
}
8+
fn foo_with_multi_lined(a: u32, b: u32, c: u32) {
9+
// ...
10+
}
11+
fn foo<T>(bar: T) {
12+
// ...
13+
}
14+
fn foo<T>(a: T, b: u32, c: u32) {
15+
// ...
16+
}
17+
fn foo<T: Foo + Bar, F: FooBar>(a: T, b: u32, c: u32) {
18+
// ...
19+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// rustfmt-space_before_fn_sig_paren: true
2+
// Function space before function paren
3+
4+
fn lorem() {
5+
// body
6+
}
7+
8+
fn lorem(ipsum: usize) {
9+
// body
10+
}
11+
12+
fn lorem<T>(ipsum: T)
13+
where
14+
T: Add + Sub + Mul + Div,
15+
{
16+
// body
17+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// rustfmt-space_before_fn_sig_paren: true
2+
// rustfmt-indent_style: Visual
3+
// rustfmt-max_width: 30
4+
// Function space before function paren
5+
6+
fn foo() {
7+
// ...
8+
}
9+
fn foo_with_multi_lined(a: u32, b: u32, c: u32) {
10+
// ...
11+
}
12+
fn foo<T>(bar: T) {
13+
// ...
14+
}
15+
fn foo<T>(a: T, b: u32, c: u32) {
16+
// ...
17+
}
18+
fn foo<T: Foo + Bar, F: FooBar>(a: T, b: u32, c: u32) {
19+
// ...
20+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// rustfmt-space_before_fn_sig_paren: true
2+
// rustfmt-max_width: 118
3+
// Trait space before function paren
4+
5+
trait Story {
6+
fn swap_context<T: 'static + Context + Send + Sync>(&mut self, context: T) -> Option<Box<Context + Send + Sync>>;
7+
}
8+
9+
impl Story for () {
10+
fn swap_context<T: 'static + Context + Send + Sync>(&mut self, context: T) -> Option<Box<Context + Send + Sync>> {
11+
// ...
12+
}
13+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// rustfmt-space_before_fn_sig_paren: true
2+
// Trait space before function paren
3+
4+
trait Story {
5+
fn swap_context<T>(&mut self, context: T) -> Option<Box<Context>>
6+
where
7+
T: Context;
8+
}
9+
10+
impl Story {
11+
fn swap_context<T>(&mut self, context: T) -> Option<Box<Context>>
12+
where
13+
T: Context,
14+
{
15+
// ...
16+
}
17+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// rustfmt-space_before_fn_sig_paren: true
2+
// rustfmt-max_width: 30
3+
// Function space before function paren
4+
5+
fn foo () {
6+
// ...
7+
}
8+
fn foo_with_multi_lined (
9+
a: u32,
10+
b: u32,
11+
c: u32,
12+
) {
13+
// ...
14+
}
15+
fn foo<T> (bar: T) {
16+
// ...
17+
}
18+
fn foo<T> (
19+
a: T,
20+
b: u32,
21+
c: u32,
22+
) {
23+
// ...
24+
}
25+
fn foo<
26+
T: Foo + Bar,
27+
F: FooBar,
28+
> (
29+
a: T,
30+
b: u32,
31+
c: u32,
32+
) {
33+
// ...
34+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// rustfmt-space_before_fn_sig_paren: true
2+
// Function space before function paren
3+
4+
fn lorem () {
5+
// body
6+
}
7+
8+
fn lorem (ipsum: usize) {
9+
// body
10+
}
11+
12+
fn lorem<T> (ipsum: T)
13+
where
14+
T: Add + Sub + Mul + Div,
15+
{
16+
// body
17+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// rustfmt-space_before_fn_sig_paren: true
2+
// rustfmt-indent_style: Visual
3+
// rustfmt-max_width: 30
4+
// Function space before function paren
5+
6+
fn foo () {
7+
// ...
8+
}
9+
fn foo_with_multi_lined (a: u32,
10+
b: u32,
11+
c: u32)
12+
{
13+
// ...
14+
}
15+
fn foo<T> (bar: T) {
16+
// ...
17+
}
18+
fn foo<T> (a: T,
19+
b: u32,
20+
c: u32) {
21+
// ...
22+
}
23+
fn foo<T: Foo + Bar,
24+
F: FooBar> (
25+
a: T,
26+
b: u32,
27+
c: u32) {
28+
// ...
29+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// rustfmt-space_before_fn_sig_paren: true
2+
// rustfmt-max_width: 118
3+
// Trait space before function paren
4+
5+
trait Story {
6+
fn swap_context<T: 'static + Context + Send + Sync> (&mut self, context: T)
7+
-> Option<Box<Context + Send + Sync>>;
8+
}
9+
10+
impl Story for () {
11+
fn swap_context<T: 'static + Context + Send + Sync> (
12+
&mut self,
13+
context: T,
14+
) -> Option<Box<Context + Send + Sync>> {
15+
// ...
16+
}
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// rustfmt-space_before_fn_sig_paren: true
2+
// Trait space before function paren
3+
4+
trait Story {
5+
fn swap_context<T> (&mut self, context: T) -> Option<Box<Context>>
6+
where
7+
T: Context;
8+
}
9+
10+
impl Story {
11+
fn swap_context<T> (&mut self, context: T) -> Option<Box<Context>>
12+
where
13+
T: Context,
14+
{
15+
// ...
16+
}
17+
}

0 commit comments

Comments
 (0)