diff options
| author | Yacin Tmimi <yacintmimi@gmail.com> | 2023-04-01 11:02:27 -0400 |
|---|---|---|
| committer | Caleb Cartwright <calebcartwright@users.noreply.github.com> | 2023-06-20 08:26:11 -0500 |
| commit | 9386b32f5a72bb276a97461d33aab415314201d8 (patch) | |
| tree | 441f53df72c65a87293800c119ed58a2ccc2e140 | |
| parent | e4a9892b7ac85e4c1a1ef7b63e2c9fa4a5aaa786 (diff) | |
| download | rust-9386b32f5a72bb276a97461d33aab415314201d8.tar.gz rust-9386b32f5a72bb276a97461d33aab415314201d8.zip | |
wrap `else {` for long, single-lined initializer expressions
This helps to prevent max width errors.
| -rw-r--r-- | src/items.rs | 6 | ||||
| -rw-r--r-- | tests/source/let_else.rs | 12 | ||||
| -rw-r--r-- | tests/target/let_else.rs | 18 |
3 files changed, 20 insertions, 16 deletions
diff --git a/src/items.rs b/src/items.rs index c2b9a26708d..97a76f6bb32 100644 --- a/src/items.rs +++ b/src/items.rs @@ -175,8 +175,10 @@ fn same_line_else_kw_and_brace( init_shape: Shape, ) -> bool { if !init_str.contains('\n') { - // initializer expression is single lined so the "else {" should be placed on the same line - return true; + // initializer expression is single lined. The "else {" can only be placed on the same line + // as the initializer expression if there is enough room for it. + // 7 = ` else {` + return init_shape.width.saturating_sub(init_str.len()) >= 7; } // 1. The initializer expression ends with one or more `)`, `]`, `}`. diff --git a/tests/source/let_else.rs b/tests/source/let_else.rs index 78465e64edc..9c02117c6e8 100644 --- a/tests/source/let_else.rs +++ b/tests/source/let_else.rs @@ -79,18 +79,18 @@ fn unbreakable_initializer_expr_pre_formatting_let_else_length_near_max_width() fn unbreakable_initializer_expr_pre_formatting_length_up_to_opening_brace_near_max_width() { // Pre Formatting: - // The length of `(indent)let pat = init else {` is 100 (max_width) + // The length of `(indent)let pat = init else {` is 99 (< max_width) // Post Formatting: // The else keyword and opening brace remain on the same line as the initializer expr, // and the else block is formatted over multiple lines because we can't fit the // else block on the same line as the initializer expr. - let Some(x) = some_really_really_really_really_really_really_really_really_long_name____E else {return}; + let Some(x) = some_really_really_really_really_really_really_really_really_long_name___E else {return}; // Pre Formatting: // The length of `(indent)let pat = init else {` is 101 (> max_width) // Post Formatting: - // The else keyword and opening brace remain on the same line as the initializer expr, - // which leads to the `{` exceeding the max width + // The else keyword and opening brace cannot fit on the same line as the initializer expr. + // They are formatted on the next line. let Some(x) = some_really_really_really_really_really_really_really_really_long_name_____F else {return}; } @@ -98,8 +98,8 @@ fn unbreakable_initializer_expr_pre_formatting_length_through_initializer_expr_n // Pre Formatting: // The length of `(indent)let pat = init` is 99 (< max_width) // Post Formatting: - // The else keyword and opening brace remain on the same line as the initializer expr, - // which leads to the `else {` exceeding the max width + // The else keyword and opening brace cannot fit on the same line as the initializer expr. + // They are formatted on the next line. let Some(x) = some_really_really_really_really_really_really_really_really_really_long_name___G else {return}; // Pre Formatting: diff --git a/tests/target/let_else.rs b/tests/target/let_else.rs index 5ada7d44657..88115d129aa 100644 --- a/tests/target/let_else.rs +++ b/tests/target/let_else.rs @@ -130,21 +130,22 @@ fn unbreakable_initializer_expr_pre_formatting_let_else_length_near_max_width() fn unbreakable_initializer_expr_pre_formatting_length_up_to_opening_brace_near_max_width() { // Pre Formatting: - // The length of `(indent)let pat = init else {` is 100 (max_width) + // The length of `(indent)let pat = init else {` is 99 (< max_width) // Post Formatting: // The else keyword and opening brace remain on the same line as the initializer expr, // and the else block is formatted over multiple lines because we can't fit the // else block on the same line as the initializer expr. - let Some(x) = some_really_really_really_really_really_really_really_really_long_name____E else { + let Some(x) = some_really_really_really_really_really_really_really_really_long_name___E else { return; }; // Pre Formatting: // The length of `(indent)let pat = init else {` is 101 (> max_width) // Post Formatting: - // The else keyword and opening brace remain on the same line as the initializer expr, - // which leads to the `{` exceeding the max width - let Some(x) = some_really_really_really_really_really_really_really_really_long_name_____F else { + // The else keyword and opening brace cannot fit on the same line as the initializer expr. + // They are formatted on the next line. + let Some(x) = some_really_really_really_really_really_really_really_really_long_name_____F + else { return; }; } @@ -153,9 +154,10 @@ fn unbreakable_initializer_expr_pre_formatting_length_through_initializer_expr_n // Pre Formatting: // The length of `(indent)let pat = init` is 99 (< max_width) // Post Formatting: - // The else keyword and opening brace remain on the same line as the initializer expr, - // which leads to the `else {` exceeding the max width - let Some(x) = some_really_really_really_really_really_really_really_really_really_long_name___G else { + // The else keyword and opening brace cannot fit on the same line as the initializer expr. + // They are formatted on the next line. + let Some(x) = some_really_really_really_really_really_really_really_really_really_long_name___G + else { return; }; |
