about summary refs log tree commit diff
diff options
context:
space:
mode:
authorYacin Tmimi <yacintmimi@gmail.com>2023-02-12 14:31:38 -0500
committerCaleb Cartwright <calebcartwright@users.noreply.github.com>2023-06-20 08:26:11 -0500
commit8be748dbb7bd95547bc770e2b0c75b7568ba97e6 (patch)
treea66c71e56d13455d593d450d6dfcc9283f09dc9b
parent9316df0ca2e709258d0d146fb311b24222116547 (diff)
downloadrust-8be748dbb7bd95547bc770e2b0c75b7568ba97e6.tar.gz
rust-8be748dbb7bd95547bc770e2b0c75b7568ba97e6.zip
Allow callers to determine if blocks can be formatted on a single line
This will make it easier to format the divergent blocks of `let-else`
statements since it'll be easier to prevent the block from being
formatted on a single line if the preconditions aren't met.
-rw-r--r--src/expr.rs13
1 files changed, 12 insertions, 1 deletions
diff --git a/src/expr.rs b/src/expr.rs
index aa81d6a93cc..eadaf206781 100644
--- a/src/expr.rs
+++ b/src/expr.rs
@@ -576,6 +576,17 @@ fn rewrite_block(
     context: &RewriteContext<'_>,
     shape: Shape,
 ) -> Option<String> {
+    rewrite_block_inner(block, attrs, label, true, context, shape)
+}
+
+fn rewrite_block_inner(
+    block: &ast::Block,
+    attrs: Option<&[ast::Attribute]>,
+    label: Option<ast::Label>,
+    allow_single_line: bool,
+    context: &RewriteContext<'_>,
+    shape: Shape,
+) -> Option<String> {
     let prefix = block_prefix(context, block, shape)?;
 
     // shape.width is used only for the single line case: either the empty block `{}`,
@@ -586,7 +597,7 @@ fn rewrite_block(
 
     let result = rewrite_block_with_visitor(context, &prefix, block, attrs, label, shape, true);
     if let Some(ref result_str) = result {
-        if result_str.lines().count() <= 3 {
+        if allow_single_line && result_str.lines().count() <= 3 {
             if let rw @ Some(_) =
                 rewrite_single_line_block(context, &prefix, block, attrs, label, shape)
             {