Skip to content

Declare & implement space_around_attr_eq config. #4040

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 4 commits into from
Apr 16, 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
22 changes: 22 additions & 0 deletions Configurations.md
Original file line number Diff line number Diff line change
Expand Up @@ -2001,6 +2001,28 @@ fn main() {
}
```

## `space_around_attr_eq`

Determines if '=' are wrapped in spaces in attributes.

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

#### `true` (default):

```rust
#[cfg(not(target_os = "pi"))]
println!("os is not pi!");
```

#### `false`

```rust
#[cfg(not(target_os="pi"))]
println!("os is not pi!");
```

## `struct_field_align_threshold`

The maximum diff of width between struct fields to be aligned with each other.
Expand Down
3 changes: 3 additions & 0 deletions rustfmt-core/rustfmt-config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ create_config! {
"Determines if '+' or '=' are wrapped in spaces in the punctuation of types";
space_before_colon: bool, false, false, "Leave a space before the colon";
space_after_colon: bool, true, false, "Leave a space after the colon";
space_around_attr_eq: bool, true, false,
"Determines if '=' are wrapped in spaces in attributes.";
spaces_around_ranges: bool, false, false, "Put spaces around the .. and ..= range operators";
binop_separator: SeparatorPlace, SeparatorPlace::Front, false,
"Where to put a binary operator when a binary expression goes multiline";
Expand Down Expand Up @@ -517,6 +519,7 @@ reorder_impl_items = false
type_punctuation_density = "Wide"
space_before_colon = false
space_after_colon = true
space_around_attr_eq = true
spaces_around_ranges = false
binop_separator = "Front"
remove_nested_parens = true
Expand Down
6 changes: 5 additions & 1 deletion rustfmt-core/rustfmt-lib/src/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,11 @@ impl Rewrite for ast::MetaItem {
// See #2479 for example.
let value = rewrite_literal(context, literal, lit_shape)
.unwrap_or_else(|| context.snippet(literal.span).to_owned());
format!("{} = {}", path, value)
if context.config.space_around_attr_eq() {
format!("{} = {}", path, value)
} else {
format!("{}={}", path, value)
}
}
})
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// rustfmt-space_around_attr_eq: false

fn attr_eq_test() {
#[cfg(not(target_os = "pi"))]
println!("os is not pi!");
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// rustfmt-space_around_attr_eq: true

fn attr_eq_test() {
#[cfg(not(target_os="pi"))]
println!("os is not pi!");
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// rustfmt-space_around_attr_eq: false

fn attr_eq_test() {
#[cfg(not(target_os="pi"))]
println!("os is not pi!");
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// rustfmt-space_around_attr_eq: true

fn attr_eq_test() {
#[cfg(not(target_os = "pi"))]
println!("os is not pi!");
}