Skip to content

Commit d418057

Browse files
ytmimicalebcartwright
authored andcommitted
Prevent structs with ".." from being rewritten with alignment
rustfmt should only support rewriting a struct in an expression position with alignment (non-default behavior) when there is no rest (with or without a base) and all of the fields are non-shorthand.
1 parent 8b58cce commit d418057

15 files changed

+458
-7
lines changed

src/expr.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -1528,12 +1528,12 @@ fn rewrite_struct_lit<'a>(
15281528
let path_shape = shape.sub_width(2)?;
15291529
let path_str = rewrite_path(context, PathContext::Expr, None, path, path_shape)?;
15301530

1531-
let has_base = match struct_rest {
1531+
let has_base_or_rest = match struct_rest {
15321532
ast::StructRest::None if fields.is_empty() => return Some(format!("{} {{}}", path_str)),
15331533
ast::StructRest::Rest(_) if fields.is_empty() => {
15341534
return Some(format!("{} {{ .. }}", path_str));
15351535
}
1536-
ast::StructRest::Base(_) => true,
1536+
ast::StructRest::Rest(_) | ast::StructRest::Base(_) => true,
15371537
_ => false,
15381538
};
15391539

@@ -1542,7 +1542,7 @@ fn rewrite_struct_lit<'a>(
15421542

15431543
let one_line_width = h_shape.map_or(0, |shape| shape.width);
15441544
let body_lo = context.snippet_provider.span_after(span, "{");
1545-
let fields_str = if struct_lit_can_be_aligned(fields, has_base)
1545+
let fields_str = if struct_lit_can_be_aligned(fields, has_base_or_rest)
15461546
&& context.config.struct_field_align_threshold() > 0
15471547
{
15481548
rewrite_with_alignment(
@@ -1614,10 +1614,7 @@ fn rewrite_struct_lit<'a>(
16141614
nested_shape,
16151615
tactic,
16161616
context,
1617-
force_no_trailing_comma
1618-
|| has_base
1619-
|| !context.use_block_indent()
1620-
|| matches!(struct_rest, ast::StructRest::Rest(_)),
1617+
force_no_trailing_comma || has_base_or_rest || !context.use_block_indent(),
16211618
);
16221619

16231620
write_list(&item_vec, &fmt)?
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
2+
// rustfmt-struct_field_align_threshold: 30
3+
4+
struct X {
5+
a: i32,
6+
b: i32,
7+
c: i32,
8+
}
9+
10+
fn test(x: X) {
11+
let d = {
12+
let e = {
13+
let f = {
14+
let g = {
15+
let h = {
16+
let i = {
17+
let j = {
18+
matches!(
19+
x,
20+
X { a: 1_000, b: 1_000, .. }
21+
)
22+
};
23+
j
24+
};
25+
i
26+
};
27+
h
28+
};
29+
g
30+
};
31+
f
32+
};
33+
e
34+
};
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// rustfmt-struct_field_align_threshold: 30
2+
3+
struct X {
4+
really_really_long_field_a: i32,
5+
really_really_really_long_field_b: i32,
6+
really_really_really_really_long_field_c: i32,
7+
really_really_really_really_really_long_field_d: i32,
8+
really_really_really_really_really_really_long_field_e: i32,
9+
f: i32,
10+
}
11+
12+
fn test(x: X) {
13+
let d = {
14+
let e = {
15+
let f = {
16+
let g = {
17+
let h = {
18+
let i = {
19+
let j = {
20+
matches!(
21+
x,
22+
X {
23+
really_really_long_field_a: 10,
24+
really_really_really_long_field_b: 10,
25+
really_really_really_really_long_field_c: 10,
26+
really_really_really_really_really_long_field_d: 10,
27+
really_really_really_really_really_really_long_field_e: 10, ..
28+
}
29+
)
30+
};
31+
j
32+
};
33+
i
34+
};
35+
h
36+
};
37+
g
38+
};
39+
f
40+
};
41+
e
42+
};
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// rustfmt-struct_field_align_threshold: 30
2+
3+
struct X {
4+
a: i32,
5+
b: i32,
6+
c: i32,
7+
d: i32,
8+
e: i32,
9+
f: i32,
10+
g: i32,
11+
h: i32,
12+
i: i32,
13+
j: i32,
14+
k: i32,
15+
}
16+
17+
fn test(x: X) {
18+
let d = {
19+
let e = {
20+
let f = {
21+
let g = {
22+
let h = {
23+
let i = {
24+
let j = {
25+
matches!(
26+
x,
27+
X {
28+
a: 1_000, b: 1_000, c: 1_000, d: 1_000, e: 1_000, f: 1_000, g: 1_000, h: 1_000, i: 1_000, j: 1_000, ..
29+
}
30+
)
31+
};
32+
j
33+
};
34+
i
35+
};
36+
h
37+
};
38+
g
39+
};
40+
f
41+
};
42+
e
43+
};
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// rustfmt-struct_field_align_threshold: 30
2+
// rustfmt-enum_discrim_align_threshold: 30
3+
// rustfmt-imports_layout: HorizontalVertical
4+
5+
#[derive(Default)]
6+
struct InnerStructA { bbbbbbbbb: i32, cccccccc: i32 }
7+
8+
enum SomeEnumNamedD {
9+
E(InnerStructA),
10+
F {
11+
ggggggggggggggggggggggggg: bool,
12+
h: bool,
13+
}
14+
}
15+
16+
impl SomeEnumNamedD {
17+
fn f_variant() -> Self {
18+
Self::F { ggggggggggggggggggggggggg: true, h: true }
19+
}
20+
}
21+
22+
fn main() {
23+
let kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk = SomeEnumNamedD::f_variant();
24+
let something_we_care_about = matches!(
25+
kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk,
26+
SomeEnumNamedD::F {
27+
ggggggggggggggggggggggggg: true,
28+
..
29+
}
30+
);
31+
32+
if something_we_care_about {
33+
println!("Yup it happened");
34+
}
35+
}
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// rustfmt-struct_field_align_threshold: 30
2+
3+
struct X { a: i32, b: i32 }
4+
5+
fn test(x: X) {
6+
let y = matches!(x, X {
7+
a: 1,
8+
..
9+
});
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// rustfmt-struct_field_align_threshold: 30
2+
3+
struct X {
4+
really_really_long_field_a: i32,
5+
really_really_really_long_field_b: i32,
6+
really_really_really_really_long_field_c: i32,
7+
really_really_really_really_really_long_field_d: i32,
8+
really_really_really_really_really_really_long_field_e: i32,
9+
f: i32,
10+
}
11+
12+
fn test(x: X) {
13+
let y = matches!(x, X {
14+
really_really_long_field_a: 10,
15+
really_really_really_long_field_b: 10,
16+
really_really_really_really_long_field_c: 10,
17+
really_really_really_really_really_long_field_d: 10,
18+
really_really_really_really_really_really_long_field_e: 10,
19+
..
20+
});
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// rustfmt-struct_field_align_threshold: 30
2+
3+
struct X {
4+
a: i32,
5+
b: i32,
6+
c: i32,
7+
d: i32,
8+
e: i32,
9+
f: i32,
10+
g: i32,
11+
h: i32,
12+
i: i32,
13+
j: i32,
14+
k: i32,
15+
}
16+
17+
fn test(x: X) {
18+
let y = matches!(x, X {
19+
a: 1_000, b: 1_000, c: 1_000, d: 1_000, e: 1_000, f: 1_000, g: 1_000, h: 1_000, i: 1_000, j: 1_000, ..
20+
});
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// rustfmt-struct_field_align_threshold: 30
2+
3+
struct X {
4+
a: i32,
5+
b: i32,
6+
c: i32,
7+
}
8+
9+
fn test(x: X) {
10+
let d = {
11+
let e = {
12+
let f = {
13+
let g = {
14+
let h = {
15+
let i = {
16+
let j = {
17+
matches!(
18+
x,
19+
X {
20+
a: 1_000,
21+
b: 1_000,
22+
..
23+
}
24+
)
25+
};
26+
j
27+
};
28+
i
29+
};
30+
h
31+
};
32+
g
33+
};
34+
f
35+
};
36+
e
37+
};
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// rustfmt-struct_field_align_threshold: 30
2+
3+
struct X {
4+
really_really_long_field_a: i32,
5+
really_really_really_long_field_b: i32,
6+
really_really_really_really_long_field_c: i32,
7+
really_really_really_really_really_long_field_d: i32,
8+
really_really_really_really_really_really_long_field_e: i32,
9+
f: i32,
10+
}
11+
12+
fn test(x: X) {
13+
let d = {
14+
let e = {
15+
let f = {
16+
let g = {
17+
let h = {
18+
let i = {
19+
let j = {
20+
matches!(
21+
x,
22+
X {
23+
really_really_long_field_a: 10,
24+
really_really_really_long_field_b: 10,
25+
really_really_really_really_long_field_c: 10,
26+
really_really_really_really_really_long_field_d: 10,
27+
really_really_really_really_really_really_long_field_e: 10,
28+
..
29+
}
30+
)
31+
};
32+
j
33+
};
34+
i
35+
};
36+
h
37+
};
38+
g
39+
};
40+
f
41+
};
42+
e
43+
};
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// rustfmt-struct_field_align_threshold: 30
2+
3+
struct X {
4+
a: i32,
5+
b: i32,
6+
c: i32,
7+
d: i32,
8+
e: i32,
9+
f: i32,
10+
g: i32,
11+
h: i32,
12+
i: i32,
13+
j: i32,
14+
k: i32,
15+
}
16+
17+
fn test(x: X) {
18+
let d = {
19+
let e = {
20+
let f = {
21+
let g = {
22+
let h = {
23+
let i = {
24+
let j = {
25+
matches!(
26+
x,
27+
X {
28+
a: 1_000,
29+
b: 1_000,
30+
c: 1_000,
31+
d: 1_000,
32+
e: 1_000,
33+
f: 1_000,
34+
g: 1_000,
35+
h: 1_000,
36+
i: 1_000,
37+
j: 1_000,
38+
..
39+
}
40+
)
41+
};
42+
j
43+
};
44+
i
45+
};
46+
h
47+
};
48+
g
49+
};
50+
f
51+
};
52+
e
53+
};
54+
}

0 commit comments

Comments
 (0)