Skip to content

Commit b9b3baf

Browse files
committed
handle field attributes when aligning a struct's fields
the `items::rewrite_struct_field` method was already handling multiline field names via `last_line_width`, which was rendered moot by the previous logic in `vertical::AlignedItem::struct_field_prefix_max_min_width`. Close #2869
1 parent da1e3c9 commit b9b3baf

File tree

5 files changed

+82
-12
lines changed

5 files changed

+82
-12
lines changed

src/items.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1590,7 +1590,7 @@ pub fn rewrite_struct_field(
15901590
shape,
15911591
attrs_extendable,
15921592
)?;
1593-
let overhead = last_line_width(&attr_prefix);
1593+
let overhead = trimmed_last_line_width(&attr_prefix);
15941594
let lhs_offset = lhs_max_width.saturating_sub(overhead);
15951595
for _ in 0..lhs_offset {
15961596
spacing.push(' ');

src/vertical.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ use crate::rewrite::{Rewrite, RewriteContext};
1717
use crate::shape::{Indent, Shape};
1818
use crate::source_map::SpanUtils;
1919
use crate::spanned::Spanned;
20-
use crate::utils::{contains_skip, is_attributes_extendable, mk_sp, rewrite_ident};
20+
use crate::utils::{
21+
contains_skip, is_attributes_extendable, mk_sp, rewrite_ident, trimmed_last_line_width,
22+
};
2123

2224
pub trait AlignedItem {
2325
fn skip(&self) -> bool;
@@ -183,13 +185,9 @@ fn struct_field_prefix_max_min_width<T: AlignedItem>(
183185
fields
184186
.iter()
185187
.map(|field| {
186-
field.rewrite_prefix(context, shape).and_then(|field_str| {
187-
if field_str.contains('\n') {
188-
None
189-
} else {
190-
Some(field_str.len())
191-
}
192-
})
188+
field
189+
.rewrite_prefix(context, shape)
190+
.map(|field_str| trimmed_last_line_width(&field_str))
193191
})
194192
.fold_options((0, ::std::usize::MAX), |(max_len, min_len), len| {
195193
(cmp::max(max_len, len), cmp::min(min_len, len))

tests/source/issue-2869.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// rustfmt-struct_field_align_threshold: 50
2+
3+
#[derive(Serialize, Deserialize, Debug)]
4+
#[serde(rename_all = "PascalCase")]
5+
struct AuditLog1 {
6+
creation_time: String,
7+
id: String,
8+
operation: String,
9+
organization_id: String,
10+
record_type: u32,
11+
result_status: Option<String>,
12+
#[serde(rename = "ClientIP")]
13+
client_ip: Option<IpAddr>,
14+
object_id: String,
15+
actor: Option<Vec<IDType>>,
16+
actor_context_id: Option<String>,
17+
actor_ip_address: Option<IpAddr>,
18+
azure_active_directory_event_type: Option<u8>,
19+
}
20+
21+
#[derive(Serialize, Deserialize, Debug)]
22+
#[serde(rename_all = "PascalCase")]
23+
struct AuditLog2 {
24+
creation_time: String,
25+
id: String,
26+
operation: String,
27+
organization_id: String,
28+
record_type: u32,
29+
result_status: Option<String>,
30+
client_ip: Option<IpAddr>,
31+
object_id: String,
32+
actor: Option<Vec<IDType>>,
33+
actor_context_id: Option<String>,
34+
actor_ip_address: Option<IpAddr>,
35+
azure_active_directory_event_type: Option<u8>,
36+
}

tests/target/configs/struct_field_align_threshold/20.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ pub struct Foo {
4141
f: SomeType, // Comment beside a field
4242
// Comment on a field
4343
#[AnAttribute]
44-
g: SomeOtherType,
44+
g: SomeOtherType,
4545
/// A doc comment on a field
46-
h: AThirdType,
46+
h: AThirdType,
4747
pub i: TypeForPublicField,
4848
}
4949

@@ -66,7 +66,7 @@ struct X {
6666
pub struct Writebatch<K: Key> {
6767
#[allow(dead_code)] // only used for holding the internal pointer
6868
writebatch: RawWritebatch,
69-
marker: PhantomData<K>,
69+
marker: PhantomData<K>,
7070
}
7171

7272
struct Bar;

tests/target/issue-2869.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// rustfmt-struct_field_align_threshold: 50
2+
3+
#[derive(Serialize, Deserialize, Debug)]
4+
#[serde(rename_all = "PascalCase")]
5+
struct AuditLog1 {
6+
creation_time: String,
7+
id: String,
8+
operation: String,
9+
organization_id: String,
10+
record_type: u32,
11+
result_status: Option<String>,
12+
#[serde(rename = "ClientIP")]
13+
client_ip: Option<IpAddr>,
14+
object_id: String,
15+
actor: Option<Vec<IDType>>,
16+
actor_context_id: Option<String>,
17+
actor_ip_address: Option<IpAddr>,
18+
azure_active_directory_event_type: Option<u8>,
19+
}
20+
21+
#[derive(Serialize, Deserialize, Debug)]
22+
#[serde(rename_all = "PascalCase")]
23+
struct AuditLog2 {
24+
creation_time: String,
25+
id: String,
26+
operation: String,
27+
organization_id: String,
28+
record_type: u32,
29+
result_status: Option<String>,
30+
client_ip: Option<IpAddr>,
31+
object_id: String,
32+
actor: Option<Vec<IDType>>,
33+
actor_context_id: Option<String>,
34+
actor_ip_address: Option<IpAddr>,
35+
azure_active_directory_event_type: Option<u8>,
36+
}

0 commit comments

Comments
 (0)