Skip to content

Commit a80e63a

Browse files
committed
Add additional test cases
These test cases try to cover various edge cases. For example, comments around the else keyword and long, unbreakable, single-line initializer expressions, and long patterns.
1 parent 4dc01f9 commit a80e63a

File tree

2 files changed

+315
-0
lines changed

2 files changed

+315
-0
lines changed

tests/source/let_else.rs

+133
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,147 @@
11
fn main() {
2+
// Although this won't compile it still parses so make sure we can format empty else blocks
3+
let Some(x) = opt else {};
4+
5+
// let-else may be formatted on a single line if they are "short"
6+
// and only contain a single expression
27
let Some(x) = opt else { return };
38

9+
let Some(x) = opt else {
10+
return
11+
};
12+
413
let Some(x) = opt else { return; };
514

615
let Some(x) = opt else {
716
// nope
817
return;
918
};
1019

20+
let Some(x) = opt else { let y = 1; return y };
21+
1122
let Some(x) = y.foo("abc", fairly_long_identifier, "def", "123456", "string", "cheese") else { bar() };
1223

1324
let Some(x) = abcdef().foo("abc", some_really_really_really_long_ident, "ident", "123456").bar().baz().qux("fffffffffffffffff") else { foo_bar() };
1425
}
26+
27+
fn with_comments_around_else_keyword() {
28+
let Some(x) = opt /* pre else keyword block-comment */ else { return };
29+
30+
let Some(x) = opt else /* post else keyword block-comment */ { return };
31+
32+
let Some(x) = opt /* pre else keyword block-comment */ else /* post else keyword block-comment */ { return };
33+
34+
let Some(x) = opt // pre else keyword line-comment
35+
else { return };
36+
37+
let Some(x) = opt else
38+
// post else keyword line-comment
39+
{ return };
40+
41+
let Some(x) = opt // pre else keyword line-comment
42+
else
43+
// post else keyword line-comment
44+
{ return };
45+
46+
}
47+
48+
fn unbreakable_initializer_expr_pre_formatting_let_else_length_near_max_width() {
49+
// Pre Formatting:
50+
// The length of `(indent)let pat = init else block;` is 100 (max_width)
51+
// Post Formatting:
52+
// The formatting is left unchanged!
53+
let Some(x) = some_really_really_really_really_really_really_really_long_name_A else { return };
54+
55+
// Pre Formatting:
56+
// The length of `(indent)let pat = init else block;` is 100 (max_width)
57+
// Post Formatting:
58+
// The else keyword and opening brace remain on the same line as the initializer expr,
59+
// and the else block is formatted over multiple lines because we can't fit the
60+
// else block on the same line as the initializer expr.
61+
let Some(x) = some_really_really_really_really_really_really_really_long_name___B else {return};
62+
63+
// Pre Formatting:
64+
// The length of `(indent)let pat = init else block;` is 100 (max_width)
65+
// Post Formatting:
66+
// The else keyword and opening brace remain on the same line as the initializer expr,
67+
// and the else block is formatted over multiple lines because we can't fit the
68+
// else block on the same line as the initializer expr.
69+
let Some(x) = some_really_really_really_really_long_name_____C else {some_divergent_function()};
70+
71+
// Pre Formatting:
72+
// The length of `(indent)let pat = init else block;` is 101 (> max_width)
73+
// Post Formatting:
74+
// The else keyword and opening brace remain on the same line as the initializer expr,
75+
// and the else block is formatted over multiple lines because we can't fit the
76+
// else block on the same line as the initializer expr.
77+
let Some(x) = some_really_really_really_really_really_really_really_long_name__D else { return };
78+
}
79+
80+
fn unbreakable_initializer_expr_pre_formatting_length_up_to_opening_brace_near_max_width() {
81+
// Pre Formatting:
82+
// The length of `(indent)let pat = init else {` is 100 (max_width)
83+
// Post Formatting:
84+
// The else keyword and opening brace remain on the same line as the initializer expr,
85+
// and the else block is formatted over multiple lines because we can't fit the
86+
// else block on the same line as the initializer expr.
87+
let Some(x) = some_really_really_really_really_really_really_really_really_long_name____E else {return};
88+
89+
// Pre Formatting:
90+
// The length of `(indent)let pat = init else {` is 101 (> max_width)
91+
// Post Formatting:
92+
// The else keyword and opening brace remain on the same line as the initializer expr,
93+
// which leads to the `{` exceeding the max width
94+
let Some(x) = some_really_really_really_really_really_really_really_really_long_name_____F else {return};
95+
}
96+
97+
fn unbreakable_initializer_expr_pre_formatting_length_through_initializer_expr_near_max_width() {
98+
// Pre Formatting:
99+
// The length of `(indent)let pat = init` is 99 (< max_width)
100+
// Post Formatting:
101+
// The else keyword and opening brace remain on the same line as the initializer expr,
102+
// which leads to the `else {` exceeding the max width
103+
let Some(x) = some_really_really_really_really_really_really_really_really_really_long_name___G else {return};
104+
105+
// Pre Formatting:
106+
// The length of `(indent)let pat = init` is 100 (max_width)
107+
// Post Formatting:
108+
// Break after the `=` and put the initializer expr on it's own line.
109+
// Because the initializer expr is multi-lined the else is placed on it's own line.
110+
let Some(x) = some_really_really_really_really_really_really_really_really_really_long_name____H else {return};
111+
112+
// Pre Formatting:
113+
// The length of `(indent)let pat = init` is 109 (> max_width)
114+
// Post Formatting:
115+
// Break after the `=` and put the initializer expr on it's own line.
116+
// Because the initializer expr is multi-lined the else is placed on it's own line.
117+
// The initializer expr has a length of 91, which when indented on the next line
118+
// The `(indent)init` line has a lengh of 99. This is the max length that the `init` can be
119+
// before we start running into max_width issues. I suspect this is becuase the shape is
120+
// accounting for the `;` at the end of the `let-else` statement.
121+
let Some(x) = some_really_really_really_really_really_really_really_really_really_really_long_name______I else {return};
122+
123+
// Pre Formatting:
124+
// The length of `(indent)let pat = init` is 110 (> max_width)
125+
// Post Formatting:
126+
// Max length issues prevent us from formatting.
127+
// The initializer expr has a length of 92, which if it would be indented on the next line
128+
// the `(indent)init` line has a lengh of 100 which == max_width of 100.
129+
// One might expect formatting to succeed, but I suspect the reason we hit max_width issues is
130+
// because the Shape is accounting for the `;` at the end of the `let-else` statement.
131+
let Some(x) = some_really_really_really_really_really_really_really_really_really_really_really_long_nameJ else {return};
132+
}
133+
134+
fn long_patterns() {
135+
let Foo {x: Bar(..), y: FooBar(..), z: Baz(..)} = opt else {
136+
return;
137+
};
138+
139+
// with version=One we don't wrap long array patterns
140+
let [aaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbb, cccccccccccccccccc, dddddddddddddddddd] = opt else {
141+
return;
142+
};
143+
144+
let Some(Ok((Message::ChangeColor(super::color::Color::Rgb(r, g, b)), Point { x, y, z }))) = opt else {
145+
return;
146+
};
147+
}

tests/target/let_else.rs

+182
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11
fn main() {
2+
// Although this won't compile it still parses so make sure we can format empty else blocks
3+
let Some(x) = opt else {};
4+
5+
// let-else may be formatted on a single line if they are "short"
6+
// and only contain a single expression
7+
let Some(x) = opt else { return };
8+
29
let Some(x) = opt else { return };
310

411
let Some(x) = opt else {
@@ -10,6 +17,11 @@ fn main() {
1017
return;
1118
};
1219

20+
let Some(x) = opt else {
21+
let y = 1;
22+
return y;
23+
};
24+
1325
let Some(x) = y.foo(
1426
"abc",
1527
fairly_long_identifier,
@@ -35,3 +47,173 @@ fn main() {
3547
foo_bar()
3648
};
3749
}
50+
51+
fn with_comments_around_else_keyword() {
52+
let Some(x) = opt
53+
/* pre else keyword block-comment */
54+
else {
55+
return;
56+
};
57+
58+
let Some(x) = opt else
59+
/* post else keyword block-comment */
60+
{
61+
return;
62+
};
63+
64+
let Some(x) = opt
65+
/* pre else keyword block-comment */
66+
else
67+
/* post else keyword block-comment */
68+
{
69+
return;
70+
};
71+
72+
let Some(x) = opt
73+
// pre else keyword line-comment
74+
else {
75+
return;
76+
};
77+
78+
let Some(x) = opt else
79+
// post else keyword line-comment
80+
{
81+
return;
82+
};
83+
84+
let Some(x) = opt
85+
// pre else keyword line-comment
86+
else
87+
// post else keyword line-comment
88+
{
89+
return;
90+
};
91+
}
92+
93+
fn unbreakable_initializer_expr_pre_formatting_let_else_length_near_max_width() {
94+
// Pre Formatting:
95+
// The length of `(indent)let pat = init else block;` is 100 (max_width)
96+
// Post Formatting:
97+
// The formatting is left unchanged!
98+
let Some(x) = some_really_really_really_really_really_really_really_long_name_A else { return };
99+
100+
// Pre Formatting:
101+
// The length of `(indent)let pat = init else block;` is 100 (max_width)
102+
// Post Formatting:
103+
// The else keyword and opening brace remain on the same line as the initializer expr,
104+
// and the else block is formatted over multiple lines because we can't fit the
105+
// else block on the same line as the initializer expr.
106+
let Some(x) = some_really_really_really_really_really_really_really_long_name___B else {
107+
return;
108+
};
109+
110+
// Pre Formatting:
111+
// The length of `(indent)let pat = init else block;` is 100 (max_width)
112+
// Post Formatting:
113+
// The else keyword and opening brace remain on the same line as the initializer expr,
114+
// and the else block is formatted over multiple lines because we can't fit the
115+
// else block on the same line as the initializer expr.
116+
let Some(x) = some_really_really_really_really_long_name_____C else {
117+
some_divergent_function()
118+
};
119+
120+
// Pre Formatting:
121+
// The length of `(indent)let pat = init else block;` is 101 (> max_width)
122+
// Post Formatting:
123+
// The else keyword and opening brace remain on the same line as the initializer expr,
124+
// and the else block is formatted over multiple lines because we can't fit the
125+
// else block on the same line as the initializer expr.
126+
let Some(x) = some_really_really_really_really_really_really_really_long_name__D else {
127+
return;
128+
};
129+
}
130+
131+
fn unbreakable_initializer_expr_pre_formatting_length_up_to_opening_brace_near_max_width() {
132+
// Pre Formatting:
133+
// The length of `(indent)let pat = init else {` is 100 (max_width)
134+
// Post Formatting:
135+
// The else keyword and opening brace remain on the same line as the initializer expr,
136+
// and the else block is formatted over multiple lines because we can't fit the
137+
// else block on the same line as the initializer expr.
138+
let Some(x) = some_really_really_really_really_really_really_really_really_long_name____E else {
139+
return;
140+
};
141+
142+
// Pre Formatting:
143+
// The length of `(indent)let pat = init else {` is 101 (> max_width)
144+
// Post Formatting:
145+
// The else keyword and opening brace remain on the same line as the initializer expr,
146+
// which leads to the `{` exceeding the max width
147+
let Some(x) = some_really_really_really_really_really_really_really_really_long_name_____F else {
148+
return;
149+
};
150+
}
151+
152+
fn unbreakable_initializer_expr_pre_formatting_length_through_initializer_expr_near_max_width() {
153+
// Pre Formatting:
154+
// The length of `(indent)let pat = init` is 99 (< max_width)
155+
// Post Formatting:
156+
// The else keyword and opening brace remain on the same line as the initializer expr,
157+
// which leads to the `else {` exceeding the max width
158+
let Some(x) = some_really_really_really_really_really_really_really_really_really_long_name___G else {
159+
return;
160+
};
161+
162+
// Pre Formatting:
163+
// The length of `(indent)let pat = init` is 100 (max_width)
164+
// Post Formatting:
165+
// Break after the `=` and put the initializer expr on it's own line.
166+
// Because the initializer expr is multi-lined the else is placed on it's own line.
167+
let Some(x) =
168+
some_really_really_really_really_really_really_really_really_really_long_name____H
169+
else {
170+
return;
171+
};
172+
173+
// Pre Formatting:
174+
// The length of `(indent)let pat = init` is 109 (> max_width)
175+
// Post Formatting:
176+
// Break after the `=` and put the initializer expr on it's own line.
177+
// Because the initializer expr is multi-lined the else is placed on it's own line.
178+
// The initializer expr has a length of 91, which when indented on the next line
179+
// The `(indent)init` line has a lengh of 99. This is the max length that the `init` can be
180+
// before we start running into max_width issues. I suspect this is becuase the shape is
181+
// accounting for the `;` at the end of the `let-else` statement.
182+
let Some(x) =
183+
some_really_really_really_really_really_really_really_really_really_really_long_name______I
184+
else {
185+
return;
186+
};
187+
188+
// Pre Formatting:
189+
// The length of `(indent)let pat = init` is 110 (> max_width)
190+
// Post Formatting:
191+
// Max length issues prevent us from formatting.
192+
// The initializer expr has a length of 92, which if it would be indented on the next line
193+
// the `(indent)init` line has a lengh of 100 which == max_width of 100.
194+
// One might expect formatting to succeed, but I suspect the reason we hit max_width issues is
195+
// because the Shape is accounting for the `;` at the end of the `let-else` statement.
196+
let Some(x) = some_really_really_really_really_really_really_really_really_really_really_really_long_nameJ else {return};
197+
}
198+
199+
fn long_patterns() {
200+
let Foo {
201+
x: Bar(..),
202+
y: FooBar(..),
203+
z: Baz(..),
204+
} = opt
205+
else {
206+
return;
207+
};
208+
209+
// with version=One we don't wrap long array patterns
210+
let [aaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbb, cccccccccccccccccc, dddddddddddddddddd] = opt else {
211+
return;
212+
};
213+
214+
let Some(Ok((Message::ChangeColor(super::color::Color::Rgb(r, g, b)), Point { x, y, z }))) =
215+
opt
216+
else {
217+
return;
218+
};
219+
}

0 commit comments

Comments
 (0)