|
1 | 1 | 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 |
2 | 7 | let Some(x) = opt else { return };
|
3 | 8 |
|
| 9 | + let Some(x) = opt else { |
| 10 | + return |
| 11 | + }; |
| 12 | + |
4 | 13 | let Some(x) = opt else { return; };
|
5 | 14 |
|
6 | 15 | let Some(x) = opt else {
|
7 | 16 | // nope
|
8 | 17 | return;
|
9 | 18 | };
|
10 | 19 |
|
| 20 | + let Some(x) = opt else { let y = 1; return y }; |
| 21 | + |
11 | 22 | let Some(x) = y.foo("abc", fairly_long_identifier, "def", "123456", "string", "cheese") else { bar() };
|
12 | 23 |
|
13 | 24 | let Some(x) = abcdef().foo("abc", some_really_really_really_long_ident, "ident", "123456").bar().baz().qux("fffffffffffffffff") else { foo_bar() };
|
14 | 25 | }
|
| 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 | +} |
0 commit comments