Skip to content

Commit d12a324

Browse files
committed
Fix Double trailing Comma (issue 4926)
I found a test case where an additional trailing comma was added in the event that struct fields were formatted on a single line. This is not the entended behavior so now we first check if a trailing comma exists before trying to add one before the ".."
1 parent 917b8a7 commit d12a324

File tree

3 files changed

+80
-1
lines changed

3 files changed

+80
-1
lines changed

src/expr.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1585,7 +1585,11 @@ fn rewrite_struct_lit<'a>(
15851585
format!("{},\n{}..", rewritten_fields, indentation)
15861586
}
15871587
} else {
1588-
format!("{}, ..", rewritten_fields)
1588+
if rewritten_fields.ends_with(",") {
1589+
format!("{} ..", rewritten_fields)
1590+
} else {
1591+
format!("{}, ..", rewritten_fields)
1592+
}
15891593
}
15901594
}
15911595
_ => rewritten_fields,
Lines changed: 35 additions & 0 deletions
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+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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 {
7+
bbbbbbbbb: i32,
8+
cccccccc: i32,
9+
}
10+
11+
enum SomeEnumNamedD {
12+
E(InnerStructA),
13+
F {
14+
ggggggggggggggggggggggggg: bool,
15+
h: bool,
16+
},
17+
}
18+
19+
impl SomeEnumNamedD {
20+
fn f_variant() -> Self {
21+
Self::F {
22+
ggggggggggggggggggggggggg: true,
23+
h: true,
24+
}
25+
}
26+
}
27+
28+
fn main() {
29+
let kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk = SomeEnumNamedD::f_variant();
30+
let something_we_care_about = matches!(
31+
kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk,
32+
SomeEnumNamedD::F {
33+
ggggggggggggggggggggggggg: true, ..
34+
}
35+
);
36+
37+
if something_we_care_about {
38+
println!("Yup it happened");
39+
}
40+
}

0 commit comments

Comments
 (0)