11fn 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" and only contain a single expression
26 let Some ( x) = opt else { return } ;
37
48 let Some ( x) = opt else { return ; } ;
@@ -8,7 +12,116 @@ fn main() {
812 return ;
913 } ;
1014
15+ let Some ( x) = opt else { let y = 1 ; return y } ;
16+
1117 let Some ( x) = y. foo ( "abc" , fairly_long_identifier, "def" , "123456" , "string" , "cheese" ) else { bar ( ) } ;
1218
1319 let Some ( x) = abcdef ( ) . foo ( "abc" , some_really_really_really_long_ident, "ident" , "123456" ) . bar ( ) . baz ( ) . qux ( "fffffffffffffffff" ) else { foo_bar ( ) } ;
1420}
21+
22+ fn with_comments_around_else_keyword ( ) {
23+ let Some ( x) = opt /* pre else keyword block-comment */ else { return } ;
24+
25+ let Some ( x) = opt else /* post else keyword block-comment */ { return } ;
26+
27+ let Some ( x) = opt /* pre else keyword block-comment */ else /* post else keyword block-comment */ { return } ;
28+
29+ let Some ( x) = opt // pre else keyword line-comment
30+ else { return } ;
31+
32+ let Some ( x) = opt else
33+ // post else keyword line-comment
34+ { return } ;
35+
36+ let Some ( x) = opt // pre else keyword line-comment
37+ else
38+ // post else keyword line-comment
39+ { return } ;
40+
41+ }
42+
43+ fn unbreakable_initializer_expr_pre_formatting_let_else_length_near_max_width ( ) {
44+ // Pre Formatting:
45+ // The length of `(indent)let pat = init else block;` is 100 (max_width)
46+ // Post Formatting:
47+ // The formatting is left unchanged!
48+ let Some ( x) = some_really_really_really_really_really_really_really_long_name_A else { return } ;
49+
50+ // Pre Formatting:
51+ // The length of `(indent)let pat = init else block;` is 100 (max_width)
52+ // Post Formatting:
53+ // The else keyword and opening brace remain on the same line as the initializer expr,
54+ // and the else block is formatted over multiple lines because we can't fit the
55+ // else block on the same line as the initializer expr.
56+ let Some ( x) = some_really_really_really_really_really_really_really_long_name___B else { return } ;
57+
58+ // Pre Formatting:
59+ // The length of `(indent)let pat = init else block;` is 100 (max_width)
60+ // Post Formatting:
61+ // The else keyword and opening brace remain on the same line as the initializer expr,
62+ // and the else block is formatted over multiple lines because we can't fit the
63+ // else block on the same line as the initializer expr.
64+ let Some ( x) = some_really_really_really_really_long_name_____C else { some_divergent_function ( ) } ;
65+
66+ // Pre Formatting:
67+ // The length of `(indent)let pat = init else block;` is 101 (> max_width)
68+ // Post Formatting:
69+ // The else keyword and opening brace remain on the same line as the initializer expr,
70+ // and the else block is formatted over multiple lines because we can't fit the
71+ // else block on the same line as the initializer expr.
72+ let Some ( x) = some_really_really_really_really_really_really_really_long_name__D else { return } ;
73+ }
74+
75+ fn unbreakable_initializer_expr_pre_formatting_length_up_to_opening_brace_near_max_width ( ) {
76+ // Pre Formatting:
77+ // The length of `(indent)let pat = init else {` is 100 (max_width)
78+ // Post Formatting:
79+ // The else keyword and opening brace remain on the same line as the initializer expr,
80+ // and the else block is formatted over multiple lines because we can't fit the
81+ // else block on the same line as the initializer expr.
82+ let Some ( x) = some_really_really_really_really_really_really_really_really_long_name____E else { return } ;
83+
84+ // Pre Formatting:
85+ // The length of `(indent)let pat = init else {` is 101 (> max_width)
86+ // Post Formatting:
87+ // The else keyword and opening brace remain on the same line as the initializer expr,
88+ // which leads to the `{` exceeding the max width
89+ let Some ( x) = some_really_really_really_really_really_really_really_really_long_name_____F else { return } ;
90+ }
91+
92+ fn unbreakable_initializer_expr_pre_formatting_length_through_initializer_expr_near_max_width ( ) {
93+ // Pre Formatting:
94+ // The length of `(indent)let pat = init` is 99 (< max_width)
95+ // Post Formatting:
96+ // The else keyword and opening brace remain on the same line as the initializer expr,
97+ // which leads to the `else {` exceeding the max width
98+ let Some ( x) = some_really_really_really_really_really_really_really_really_really_long_name___G else { return } ;
99+
100+ // Pre Formatting:
101+ // The length of `(indent)let pat = init` is 100 (max_width)
102+ // Post Formatting:
103+ // Break after the `=` and put the initializer expr on it's own line.
104+ // Because the initializer expr is multi-lined the else is placed on it's own line.
105+ let Some ( x) = some_really_really_really_really_really_really_really_really_really_long_name____H else { return } ;
106+
107+ // Pre Formatting:
108+ // The length of `(indent)let pat = init` is 109 (> max_width)
109+ // Post Formatting:
110+ // Break after the `=` and put the initializer expr on it's own line.
111+ // Because the initializer expr is multi-lined the else is placed on it's own line.
112+ // The initializer expr has a length of 91, which when indented on the next line
113+ // The `(indent)init` line has a lengh of 99. This is the max length that the `init` can be
114+ // before we start running into max_width issues. I suspect this is becuase the shape is
115+ // accounting for the `;` at the end of the `let-else` statement.
116+ let Some ( x) = some_really_really_really_really_really_really_really_really_really_really_long_name______I else { return } ;
117+
118+ // Pre Formatting:
119+ // The length of `(indent)let pat = init` is 110 (> max_width)
120+ // Post Formatting:
121+ // Max length issues prevent us from formatting.
122+ // The initializer expr has a length of 92, which if it would be indented on the next line
123+ // the `(indent)init` line has a lengh of 100 which == max_width of 100.
124+ // One might expect formatting to succeed, but I suspect the reason we hit max_width issues is
125+ // because the Shape is accounting for the `;` at the end of the `let-else` statement.
126+ let Some ( x) = some_really_really_really_really_really_really_really_really_really_really_really_long_nameJ else { return } ;
127+ }
0 commit comments