Skip to content

Put attributes and struct fields on different line by default #2357

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
Jan 12, 2018
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## [Unreleased]

### Changed

- Remove `same_line_attributes` configuration option.

## [0.3.4] 2017-12-23

### Added
Expand Down
47 changes: 0 additions & 47 deletions Configurations.md
Original file line number Diff line number Diff line change
Expand Up @@ -246,53 +246,6 @@ fn lorem<Ipsum, Dolor, Sit, Amet>() -> T
}
```


## `same_line_attributes`

Try to put attributes on the same line as fields and variants

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

#### `true` (default):

```rust
struct Lorem {
#[serde(rename = "Ipsum")] ipsum: usize,
#[serde(rename = "Dolor")] dolor: usize,
#[serde(rename = "Amet")] amet: usize,
}

enum Lorem {
#[serde(skip_serializing)] Ipsum,
#[serde(skip_serializing)] Dolor,
#[serde(skip_serializing)] Amet,
}
```

#### `false`:

```rust
struct Lorem {
#[serde(rename = "Ipsum")]
ipsum: usize,
#[serde(rename = "Dolor")]
dolor: usize,
#[serde(rename = "Amet")]
amet: usize,
}

enum Lorem {
#[serde(skip_serializing)]
Ipsum,
#[serde(skip_serializing)]
Dolor,
#[serde(skip_serializing)]
Amet,
}
```

## `use_small_heuristics`

Whether to use different formatting for items and expressions if they satisfy a heuristic notion of 'small'.
Expand Down
2 changes: 0 additions & 2 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -648,8 +648,6 @@ create_config! {
threshold.";
remove_blank_lines_at_start_or_end_of_block: bool, true, false,
"Remove blank lines at start or end of a block";
same_line_attributes: bool, true, false,
"Try to put attributes on the same line as fields and variants.";
match_arm_blocks: bool, true, false, "Wrap the body of arms in blocks when it does not fit on \
the same line with the pattern of arms";
force_multiline_blocks: bool, false, false,
Expand Down
3 changes: 2 additions & 1 deletion src/file_lines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,8 @@ impl str::FromStr for FileLines {
// For JSON decoding.
#[derive(Clone, Debug, Deserialize)]
struct JsonSpan {
#[serde(deserialize_with = "deserialize_filename")] file: FileName,
#[serde(deserialize_with = "deserialize_filename")]
file: FileName,
range: (usize, usize),
}

Expand Down
7 changes: 2 additions & 5 deletions src/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -558,15 +558,13 @@ impl<'a> FmtVisitor<'a> {
}
};

let attrs_extendable = attrs_str.is_empty()
|| (context.config.same_line_attributes() && is_attributes_extendable(&attrs_str));
combine_strs_with_missing_comments(
&context,
&attrs_str,
&variant_body,
span,
shape,
attrs_extendable,
is_attributes_extendable(&attrs_str),
)
}
}
Expand Down Expand Up @@ -1439,8 +1437,7 @@ pub fn rewrite_struct_field(
let prefix = rewrite_struct_field_prefix(context, field)?;

let attrs_str = field.attrs.rewrite(context, shape)?;
let attrs_extendable = attrs_str.is_empty()
|| (context.config.same_line_attributes() && is_attributes_extendable(&attrs_str));
let attrs_extendable = field.ident.is_none() && is_attributes_extendable(&attrs_str);
let missing_span = if field.attrs.is_empty() {
mk_sp(field.span.lo(), field.span.lo())
} else {
Expand Down
3 changes: 1 addition & 2 deletions src/vertical.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ impl AlignedItem for ast::StructField {
} else {
mk_sp(self.attrs.last().unwrap().span.hi(), self.span.lo())
};
let attrs_extendable =
context.config.same_line_attributes() && is_attributes_extendable(&attrs_str);
let attrs_extendable = self.ident.is_none() && is_attributes_extendable(&attrs_str);
rewrite_struct_field_prefix(context, self).and_then(|field_str| {
combine_strs_with_missing_comments(
context,
Expand Down
26 changes: 0 additions & 26 deletions tests/source/configs/same_line_attributes/false.rs

This file was deleted.

20 changes: 0 additions & 20 deletions tests/source/configs/same_line_attributes/true.rs

This file was deleted.

5 changes: 5 additions & 0 deletions tests/source/issue-2342.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// rustfmt-max_width: 80

struct Foo {
#[cfg(feature = "serde")] bytes: [[u8; 17]; 5], // Same size as signature::ED25519_PKCS8_V2_LEN
}
3 changes: 2 additions & 1 deletion tests/target/attrib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ impl Bar {

// #984
struct Foo {
#[derive(Clone, PartialEq, Debug, Deserialize, Serialize)] foo: usize,
#[derive(Clone, PartialEq, Debug, Deserialize, Serialize)]
foo: usize,
}

// #1668
Expand Down
26 changes: 0 additions & 26 deletions tests/target/configs/same_line_attributes/false.rs

This file was deleted.

14 changes: 0 additions & 14 deletions tests/target/configs/same_line_attributes/true.rs

This file was deleted.

3 changes: 2 additions & 1 deletion tests/target/configs/struct_field_align_threshold/20.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ pub struct Foo {
f : SomeType, // Comment beside a field
f: SomeType, // Comment beside a field
// Comment on a field
#[AnAttribute] g: SomeOtherType,
#[AnAttribute]
g: SomeOtherType,
/// A doc comment on a field
h: AThirdType,
pub i: TypeForPublicField,
Expand Down
3 changes: 2 additions & 1 deletion tests/target/enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ enum StructLikeVariants {
StructLike {
x: i32, // Test comment
// Pre-comment
#[Attr50] y: SomeType, // Aanother Comment
#[Attr50]
y: SomeType, // Aanother Comment
},
SL {
a: A,
Expand Down
6 changes: 6 additions & 0 deletions tests/target/issue-2342.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// rustfmt-max_width: 80

struct Foo {
#[cfg(feature = "serde")]
bytes: [[u8; 17]; 5], // Same size as signature::ED25519_PKCS8_V2_LEN
}
6 changes: 4 additions & 2 deletions tests/target/struct-field-attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
struct Foo {
bar: u64,

#[cfg(test)] qux: u64,
#[cfg(test)]
qux: u64,
}

fn do_something() -> Foo {
Expand All @@ -23,7 +24,8 @@ fn main() {
// #1462
struct Foo {
foo: usize,
#[cfg(feature = "include-bar")] bar: usize,
#[cfg(feature = "include-bar")]
bar: usize,
}

fn new_foo() -> Foo {
Expand Down
3 changes: 2 additions & 1 deletion tests/target/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ pub struct Foo {
f : SomeType, // Comment beside a field
f: SomeType, // Comment beside a field
// Comment on a field
#[AnAttribute] g: SomeOtherType,
#[AnAttribute]
g: SomeOtherType,
/// A doc comment on a field
h: AThirdType,
pub i: TypeForPublicField,
Expand Down
3 changes: 2 additions & 1 deletion tests/target/unions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ pub union Foo {
f : SomeType, // Comment beside a field
f: SomeType, // Comment beside a field
// Comment on a field
#[AnAttribute] g: SomeOtherType,
#[AnAttribute]
g: SomeOtherType,
/// A doc comment on a field
h: AThirdType,
pub i: TypeForPublicField,
Expand Down