about summary refs log tree commit diff
diff options
context:
space:
mode:
authorYacin Tmimi <yacintmimi@gmail.com>2023-02-12 14:46:06 -0500
committerCaleb Cartwright <calebcartwright@users.noreply.github.com>2023-06-20 08:26:11 -0500
commit7a3e4fca4003ab3f0ccfddaa0a92969e676a2881 (patch)
tree93c71cefdd1506f35b88a01d90112e03ac8752a5
parent8be748dbb7bd95547bc770e2b0c75b7568ba97e6 (diff)
downloadrust-7a3e4fca4003ab3f0ccfddaa0a92969e676a2881.tar.gz
rust-7a3e4fca4003ab3f0ccfddaa0a92969e676a2881.zip
Implement `let-else` rewriting in terms of rewrite_let_else_block
`rewrite_let_else_block` gives us more control over allowing the `else`
block to be formatted on a single line than
`<ast::Block as Rewrite>::rewrite`.
-rw-r--r--src/expr.rs10
-rw-r--r--src/items.rs11
2 files changed, 19 insertions, 2 deletions
diff --git a/src/expr.rs b/src/expr.rs
index eadaf206781..a4e60659a0e 100644
--- a/src/expr.rs
+++ b/src/expr.rs
@@ -609,6 +609,16 @@ fn rewrite_block_inner(
     result
 }
 
+/// Rewrite the divergent block of a `let-else` statement.
+pub(crate) fn rewrite_let_else_block(
+    block: &ast::Block,
+    allow_single_line: bool,
+    context: &RewriteContext<'_>,
+    shape: Shape,
+) -> Option<String> {
+    rewrite_block_inner(block, None, None, allow_single_line, context, shape)
+}
+
 // Rewrite condition if the given expression has one.
 pub(crate) fn rewrite_cond(
     context: &RewriteContext<'_>,
diff --git a/src/items.rs b/src/items.rs
index 75c42605ccf..4693e57b8a1 100644
--- a/src/items.rs
+++ b/src/items.rs
@@ -18,7 +18,8 @@ use crate::config::lists::*;
 use crate::config::{BraceStyle, Config, IndentStyle, Version};
 use crate::expr::{
     is_empty_block, is_simple_block_stmt, rewrite_assign_rhs, rewrite_assign_rhs_with,
-    rewrite_assign_rhs_with_comments, rewrite_else_kw_with_comments, RhsAssignKind, RhsTactics,
+    rewrite_assign_rhs_with_comments, rewrite_else_kw_with_comments, rewrite_let_else_block,
+    RhsAssignKind, RhsTactics,
 };
 use crate::lists::{definitive_tactic, itemize_list, write_list, ListFormatting, Separator};
 use crate::macros::{rewrite_macro, MacroPosition};
@@ -132,7 +133,13 @@ impl Rewrite for ast::Local {
                     shape,
                 );
                 result.push_str(&else_kw);
-                result.push_str(&block.rewrite(context, shape)?);
+                let allow_single_line = !result.contains('\n');
+                result.push_str(&rewrite_let_else_block(
+                    block,
+                    allow_single_line,
+                    context,
+                    shape,
+                )?);
             };
         }