diff --git a/Configurations.md b/Configurations.md index 08d7bed5df6..cb9d1c8a356 100644 --- a/Configurations.md +++ b/Configurations.md @@ -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. diff --git a/rustfmt-core/rustfmt-config/src/lib.rs b/rustfmt-core/rustfmt-config/src/lib.rs index ce3ccce99a3..bf3a7d2dbbc 100644 --- a/rustfmt-core/rustfmt-config/src/lib.rs +++ b/rustfmt-core/rustfmt-config/src/lib.rs @@ -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"; @@ -514,6 +516,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 diff --git a/rustfmt-core/rustfmt-lib/src/attr.rs b/rustfmt-core/rustfmt-lib/src/attr.rs index b9e160020fb..e868efe4d6e 100644 --- a/rustfmt-core/rustfmt-lib/src/attr.rs +++ b/rustfmt-core/rustfmt-lib/src/attr.rs @@ -248,7 +248,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) + } } }) } diff --git a/rustfmt-core/rustfmt-lib/tests/source/configs/space_around_attr_eq/false.rs b/rustfmt-core/rustfmt-lib/tests/source/configs/space_around_attr_eq/false.rs new file mode 100644 index 00000000000..af45ef1abfe --- /dev/null +++ b/rustfmt-core/rustfmt-lib/tests/source/configs/space_around_attr_eq/false.rs @@ -0,0 +1,6 @@ +// rustfmt-space_around_attr_eq: false + +fn attr_eq_test() { + #[cfg(not(target_os = "pi"))] + println!("os is not pi!"); +} diff --git a/rustfmt-core/rustfmt-lib/tests/source/configs/space_around_attr_eq/true.rs b/rustfmt-core/rustfmt-lib/tests/source/configs/space_around_attr_eq/true.rs new file mode 100644 index 00000000000..399ceaf8a96 --- /dev/null +++ b/rustfmt-core/rustfmt-lib/tests/source/configs/space_around_attr_eq/true.rs @@ -0,0 +1,6 @@ +// rustfmt-space_around_attr_eq: true + +fn attr_eq_test() { + #[cfg(not(target_os="pi"))] + println!("os is not pi!"); +} diff --git a/rustfmt-core/rustfmt-lib/tests/target/configs/space_around_attr_eq/false.rs b/rustfmt-core/rustfmt-lib/tests/target/configs/space_around_attr_eq/false.rs new file mode 100644 index 00000000000..5aac16a503b --- /dev/null +++ b/rustfmt-core/rustfmt-lib/tests/target/configs/space_around_attr_eq/false.rs @@ -0,0 +1,6 @@ +// rustfmt-space_around_attr_eq: false + +fn attr_eq_test() { + #[cfg(not(target_os="pi"))] + println!("os is not pi!"); +} diff --git a/rustfmt-core/rustfmt-lib/tests/target/configs/space_around_attr_eq/true.rs b/rustfmt-core/rustfmt-lib/tests/target/configs/space_around_attr_eq/true.rs new file mode 100644 index 00000000000..0e7bb4b42b6 --- /dev/null +++ b/rustfmt-core/rustfmt-lib/tests/target/configs/space_around_attr_eq/true.rs @@ -0,0 +1,6 @@ +// rustfmt-space_around_attr_eq: true + +fn attr_eq_test() { + #[cfg(not(target_os = "pi"))] + println!("os is not pi!"); +}