Skip to content

Commit a626b1e

Browse files
committed
Add option space_after_function_name
1 parent 8fb4fa5 commit a626b1e

File tree

10 files changed

+161
-1
lines changed

10 files changed

+161
-1
lines changed

Configurations.md

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

20642064
See also: [`space_before_colon`](#space_before_colon).
20652065

2066+
## `space_after_function_name`
2067+
2068+
Leave a space after the function name.
2069+
2070+
- **Default value**: `"Never"`
2071+
- **Possible values**: `"AfterGenerics"`, `"Never"`
2072+
- **Stable**: No (tracking issue: [#3564](https://github.com/rust-lang/rustfmt/issues/3564))
2073+
2074+
#### `"Never"` (default):
2075+
2076+
```rust
2077+
fn lorem() {
2078+
// body
2079+
}
2080+
2081+
fn lorem(ipsum: usize) {
2082+
// body
2083+
}
2084+
2085+
fn lorem<T>(ipsum: T)
2086+
where
2087+
T: Add + Sub + Mul + Div,
2088+
{
2089+
// body
2090+
}
2091+
```
2092+
2093+
#### `"AfterGenerics"`:
2094+
2095+
```rust
2096+
fn lorem () {
2097+
// body
2098+
}
2099+
2100+
fn lorem (ipsum: usize) {
2101+
// body
2102+
}
2103+
2104+
fn lorem<T> (ipsum: T)
2105+
where
2106+
T: Add + Sub + Mul + Div,
2107+
{
2108+
// body
2109+
}
2110+
```
2111+
20662112
## `space_before_colon`
20672113

20682114
Leave a space before the colon.

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_after_function_name: SpaceAfterFunctionName, SpaceAfterFunctionName::Never, false,
98+
"Leave a space after the function name.";
9799

98100
// Misc.
99101
remove_nested_parens: bool, true, true, "Remove nested parens";
@@ -597,6 +599,7 @@ space_after_colon = true
597599
space_around_attr_eq = true
598600
spaces_around_ranges = false
599601
binop_separator = "Front"
602+
space_after_function_name = "Never"
600603
remove_nested_parens = true
601604
combine_control_expr = true
602605
overflow_delimited_expr = false

src/config/options.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,15 @@ pub enum MatchArmLeadingPipe {
359359
KeepExisting,
360360
}
361361

362+
#[config_type]
363+
/// Where to add spaces after the function name.
364+
pub enum SpaceAfterFunctionName {
365+
/// Never add spaces.
366+
Never,
367+
/// Add a space, and put generics before the space.
368+
AfterGenerics,
369+
}
370+
362371
#[cfg(test)]
363372
mod test {
364373
use std::path::PathBuf;

src/formatting/items.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use rustc_ast::{ast, ptr};
99
use rustc_span::{source_map, symbol, BytePos, Span, DUMMY_SP};
1010

1111
use crate::config::lists::*;
12-
use crate::config::{BraceStyle, Config, IndentStyle};
12+
use crate::config::{BraceStyle, Config, IndentStyle, SpaceAfterFunctionName};
1313
use crate::formatting::{
1414
attr::filter_inline_attrs,
1515
comment::{
@@ -2228,6 +2228,10 @@ fn rewrite_fn_base(
22282228
.last()
22292229
.map_or(false, |l| l.trim_start().len() == 1);
22302230

2231+
if let SpaceAfterFunctionName::AfterGenerics = context.config.space_after_function_name() {
2232+
result.push(' ');
2233+
}
2234+
22312235
// Note that the width and indent don't really matter, we'll re-layout the
22322236
// return type later anyway.
22332237
let ret_str = fd
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// rustfmt-space_after_function_name: AfterGenerics
2+
// Function space after function name
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: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// rustfmt-space_after_function_name: AfterGenerics
2+
// Trait space after function name
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: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// rustfmt-space_after_function_name: AfterGenerics
2+
// rustfmt-max_width: 118
3+
// Trait space after function name
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_after_function_name: AfterGenerics
2+
// Function space after function name
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: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// rustfmt-space_after_function_name: AfterGenerics
2+
// Trait space after function name
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: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// rustfmt-space_after_function_name: AfterGenerics
2+
// rustfmt-max_width: 118
3+
// Trait space after function name
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+
}

0 commit comments

Comments
 (0)