Skip to content

Commit 9ace1de

Browse files
authored
Declare & implement space_around_attr_eq config. (#4040)
1 parent 45ac003 commit 9ace1de

File tree

7 files changed

+54
-1
lines changed

7 files changed

+54
-1
lines changed

Configurations.md

+22
Original file line numberDiff line numberDiff line change
@@ -2001,6 +2001,28 @@ fn main() {
20012001
}
20022002
```
20032003

2004+
## `space_around_attr_eq`
2005+
2006+
Determines if '=' are wrapped in spaces in attributes.
2007+
2008+
- **Default value**: `true`
2009+
- **Possible values**: `true`, `false`
2010+
- **Stable**: No
2011+
2012+
#### `true` (default):
2013+
2014+
```rust
2015+
#[cfg(not(target_os = "pi"))]
2016+
println!("os is not pi!");
2017+
```
2018+
2019+
#### `false`
2020+
2021+
```rust
2022+
#[cfg(not(target_os="pi"))]
2023+
println!("os is not pi!");
2024+
```
2025+
20042026
## `struct_field_align_threshold`
20052027

20062028
The maximum diff of width between struct fields to be aligned with each other.

rustfmt-core/rustfmt-config/src/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ create_config! {
7373
"Determines if '+' or '=' are wrapped in spaces in the punctuation of types";
7474
space_before_colon: bool, false, false, "Leave a space before the colon";
7575
space_after_colon: bool, true, false, "Leave a space after the colon";
76+
space_around_attr_eq: bool, true, false,
77+
"Determines if '=' are wrapped in spaces in attributes.";
7678
spaces_around_ranges: bool, false, false, "Put spaces around the .. and ..= range operators";
7779
binop_separator: SeparatorPlace, SeparatorPlace::Front, false,
7880
"Where to put a binary operator when a binary expression goes multiline";
@@ -514,6 +516,7 @@ reorder_impl_items = false
514516
type_punctuation_density = "Wide"
515517
space_before_colon = false
516518
space_after_colon = true
519+
space_around_attr_eq = true
517520
spaces_around_ranges = false
518521
binop_separator = "Front"
519522
remove_nested_parens = true

rustfmt-core/rustfmt-lib/src/attr.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,11 @@ impl Rewrite for ast::MetaItem {
248248
// See #2479 for example.
249249
let value = rewrite_literal(context, literal, lit_shape)
250250
.unwrap_or_else(|| context.snippet(literal.span).to_owned());
251-
format!("{} = {}", path, value)
251+
if context.config.space_around_attr_eq() {
252+
format!("{} = {}", path, value)
253+
} else {
254+
format!("{}={}", path, value)
255+
}
252256
}
253257
})
254258
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// rustfmt-space_around_attr_eq: false
2+
3+
fn attr_eq_test() {
4+
#[cfg(not(target_os = "pi"))]
5+
println!("os is not pi!");
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// rustfmt-space_around_attr_eq: true
2+
3+
fn attr_eq_test() {
4+
#[cfg(not(target_os="pi"))]
5+
println!("os is not pi!");
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// rustfmt-space_around_attr_eq: false
2+
3+
fn attr_eq_test() {
4+
#[cfg(not(target_os="pi"))]
5+
println!("os is not pi!");
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// rustfmt-space_around_attr_eq: true
2+
3+
fn attr_eq_test() {
4+
#[cfg(not(target_os = "pi"))]
5+
println!("os is not pi!");
6+
}

0 commit comments

Comments
 (0)