From 9c84d17c7c967a600fb4f29c62e08f116a9fe60c Mon Sep 17 00:00:00 2001 From: Caleb Cartwright Date: Sun, 20 Oct 2019 23:02:52 -0500 Subject: [PATCH] fix: checking block emptiness --- src/expr.rs | 17 ++++++++-- .../control-brace-style-always-next-line.rs | 14 ++++---- .../control-brace-style-always-same-line.rs | 14 ++++---- .../else-if-brace-style-always-next-line.rs | 32 +++++++++---------- .../else-if-brace-style-always-same-line.rs | 32 +++++++++---------- .../else-if-brace-style-closing-next-line.rs | 32 +++++++++---------- tests/source/issue_3868.rs | 13 ++++++++ .../control-brace-style-always-next-line.rs | 12 +++---- .../control-brace-style-always-same-line.rs | 12 +++---- .../else-if-brace-style-always-next-line.rs | 32 +++++++++---------- .../else-if-brace-style-always-same-line.rs | 32 +++++++++---------- .../else-if-brace-style-closing-next-line.rs | 32 +++++++++---------- tests/target/issue_3868.rs | 7 ++++ 13 files changed, 157 insertions(+), 124 deletions(-) create mode 100644 tests/source/issue_3868.rs create mode 100644 tests/target/issue_3868.rs diff --git a/src/expr.rs b/src/expr.rs index 8665f2a7248..108a9726be3 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -421,7 +421,7 @@ fn rewrite_empty_block( prefix: &str, shape: Shape, ) -> Option { - if !block.stmts.is_empty() { + if block_has_statements(&block) { return None; } @@ -1137,6 +1137,19 @@ pub(crate) fn is_simple_block_stmt( && attrs.map_or(true, |a| a.is_empty()) } +fn block_has_statements(block: &ast::Block) -> bool { + block.stmts.iter().any(|stmt| { + if let ast::StmtKind::Semi(ref expr) = stmt.kind { + if let ast::ExprKind::Tup(ref tup_exprs) = expr.kind { + if tup_exprs.is_empty() { + return false; + } + } + } + true + }) +} + /// Checks whether a block contains no statements, expressions, comments, or /// inner attributes. pub(crate) fn is_empty_block( @@ -1144,7 +1157,7 @@ pub(crate) fn is_empty_block( attrs: Option<&[ast::Attribute]>, source_map: &SourceMap, ) -> bool { - block.stmts.is_empty() + !block_has_statements(&block) && !block_contains_comment(block, source_map) && attrs.map_or(true, |a| inner_attributes(a).is_empty()) } diff --git a/tests/source/control-brace-style-always-next-line.rs b/tests/source/control-brace-style-always-next-line.rs index 9079fb46c0c..25661fcef7e 100644 --- a/tests/source/control-brace-style-always-next-line.rs +++ b/tests/source/control-brace-style-always-next-line.rs @@ -2,25 +2,25 @@ fn main() { loop { - (); - (); + let foo = (); + let bar = (); } - 'label: loop // loop comment + 'label: loop // loop comment { - (); + let foo = (); } cond = true; while cond { - (); + let foo = (); } 'while_label: while cond { // while comment - (); + let foo = (); } @@ -28,7 +28,7 @@ fn main() { for sub_obj in obj { 'nested_while_label: while cond { - (); + let foo = (); } } } diff --git a/tests/source/control-brace-style-always-same-line.rs b/tests/source/control-brace-style-always-same-line.rs index 45111aaab08..b5477c7aead 100644 --- a/tests/source/control-brace-style-always-same-line.rs +++ b/tests/source/control-brace-style-always-same-line.rs @@ -1,24 +1,24 @@ fn main() { loop { - (); - (); + let foo = (); + let bar = (); } - 'label: loop // loop comment + 'label: loop // loop comment { - (); + let foo = (); } cond = true; while cond { - (); + let foo = (); } 'while_label: while cond { // while comment - (); + let foo = (); } @@ -26,7 +26,7 @@ fn main() { for sub_obj in obj { 'nested_while_label: while cond { - (); + let foo = (); } } } diff --git a/tests/source/else-if-brace-style-always-next-line.rs b/tests/source/else-if-brace-style-always-next-line.rs index 7b4870fc65a..e37707f7646 100644 --- a/tests/source/else-if-brace-style-always-next-line.rs +++ b/tests/source/else-if-brace-style-always-next-line.rs @@ -3,14 +3,14 @@ fn main() { if false { - (); - (); + let foo = (); + let bar = (); } if false // lone if comment { - (); - (); + let foo = (); + let bar = (); } @@ -26,29 +26,29 @@ fn main() { if true { - (); + let foo = (); } else if false { - (); - (); + let foo = (); + let bar = (); } else { - (); - (); - (); + let foo = (); + let bar = (); + let baz = (); } if true // else-if-chain if comment { - (); + let foo = (); } else if false // else-if-chain else-if comment { - (); - (); + let foo = (); + let bar = (); } else // else-if-chain else comment { - (); - (); - (); + let foo = (); + let bar = (); + let baz = (); } } diff --git a/tests/source/else-if-brace-style-always-same-line.rs b/tests/source/else-if-brace-style-always-same-line.rs index 37c9417ea16..6322ba2e192 100644 --- a/tests/source/else-if-brace-style-always-same-line.rs +++ b/tests/source/else-if-brace-style-always-same-line.rs @@ -1,14 +1,14 @@ fn main() { if false { - (); - (); + let foo = (); + let bar = (); } if false // lone if comment { - (); - (); + let foo = (); + let bar = (); } @@ -24,29 +24,29 @@ fn main() { if true { - (); + let foo = (); } else if false { - (); - (); + let foo = (); + let bar = (); } else { - (); - (); - (); + let foo = (); + let bar = (); + let baz = (); } if true // else-if-chain if comment { - (); + let foo = (); } else if false // else-if-chain else-if comment { - (); - (); + let foo = (); + let bar = (); } else // else-if-chain else comment { - (); - (); - (); + let foo = (); + let bar = (); + let baz = (); } } diff --git a/tests/source/else-if-brace-style-closing-next-line.rs b/tests/source/else-if-brace-style-closing-next-line.rs index 3b885b3faaa..4d358ae9a3a 100644 --- a/tests/source/else-if-brace-style-closing-next-line.rs +++ b/tests/source/else-if-brace-style-closing-next-line.rs @@ -3,14 +3,14 @@ fn main() { if false { - (); - (); + let foo = (); + let bar = (); } if false // lone if comment { - (); - (); + let foo = (); + let bar = (); } @@ -26,29 +26,29 @@ fn main() { if true { - (); + let foo = (); } else if false { - (); - (); + let foo = (); + let bar = (); } else { - (); - (); - (); + let foo = (); + let bar = (); + let baz = (); } if true // else-if-chain if comment { - (); + let foo = (); } else if false // else-if-chain else-if comment { - (); - (); + let foo = (); + let bar = (); } else // else-if-chain else comment { - (); - (); - (); + let foo = (); + let bar = (); + let baz = (); } } diff --git a/tests/source/issue_3868.rs b/tests/source/issue_3868.rs new file mode 100644 index 00000000000..0bddd3afbb0 --- /dev/null +++ b/tests/source/issue_3868.rs @@ -0,0 +1,13 @@ +fn foo() { + ; +} + +fn bar() { + for _ in 0..1 { + ; + } +} + +fn baz() { + (); + } diff --git a/tests/target/control-brace-style-always-next-line.rs b/tests/target/control-brace-style-always-next-line.rs index 054a3075ca0..7b55591c6f5 100644 --- a/tests/target/control-brace-style-always-next-line.rs +++ b/tests/target/control-brace-style-always-next-line.rs @@ -3,26 +3,26 @@ fn main() { loop { - (); - (); + let foo = (); + let bar = (); } 'label: loop // loop comment { - (); + let foo = (); } cond = true; while cond { - (); + let foo = (); } 'while_label: while cond { // while comment - (); + let foo = (); } for obj in iter @@ -31,7 +31,7 @@ fn main() { { 'nested_while_label: while cond { - (); + let foo = (); } } } diff --git a/tests/target/control-brace-style-always-same-line.rs b/tests/target/control-brace-style-always-same-line.rs index cf3f82dfcf1..aaa016cf74b 100644 --- a/tests/target/control-brace-style-always-same-line.rs +++ b/tests/target/control-brace-style-always-same-line.rs @@ -1,29 +1,29 @@ fn main() { loop { - (); - (); + let foo = (); + let bar = (); } 'label: loop // loop comment { - (); + let foo = (); } cond = true; while cond { - (); + let foo = (); } 'while_label: while cond { // while comment - (); + let foo = (); } for obj in iter { for sub_obj in obj { 'nested_while_label: while cond { - (); + let foo = (); } } } diff --git a/tests/target/else-if-brace-style-always-next-line.rs b/tests/target/else-if-brace-style-always-next-line.rs index 31e12cfa0d0..740213555b7 100644 --- a/tests/target/else-if-brace-style-always-next-line.rs +++ b/tests/target/else-if-brace-style-always-next-line.rs @@ -3,51 +3,51 @@ fn main() { if false { - (); - (); + let foo = (); + let bar = (); } if false // lone if comment { - (); - (); + let foo = (); + let bar = (); } let a = if 0 > 1 { unreachable!() } else { 0x0 }; if true { - (); + let foo = (); } else if false { - (); - (); + let foo = (); + let bar = (); } else { - (); - (); - (); + let foo = (); + let bar = (); + let baz = (); } if true // else-if-chain if comment { - (); + let foo = (); } else if false // else-if-chain else-if comment { - (); - (); + let foo = (); + let bar = (); } else // else-if-chain else comment { - (); - (); - (); + let foo = (); + let bar = (); + let baz = (); } } diff --git a/tests/target/else-if-brace-style-always-same-line.rs b/tests/target/else-if-brace-style-always-same-line.rs index 07b71fd790d..bf5b9de4ce2 100644 --- a/tests/target/else-if-brace-style-always-same-line.rs +++ b/tests/target/else-if-brace-style-always-same-line.rs @@ -1,43 +1,43 @@ fn main() { if false { - (); - (); + let foo = (); + let bar = (); } if false // lone if comment { - (); - (); + let foo = (); + let bar = (); } let a = if 0 > 1 { unreachable!() } else { 0x0 }; if true { - (); + let foo = (); } else if false { - (); - (); + let foo = (); + let bar = (); } else { - (); - (); - (); + let foo = (); + let bar = (); + let baz = (); } if true // else-if-chain if comment { - (); + let foo = (); } else if false // else-if-chain else-if comment { - (); - (); + let foo = (); + let bar = (); } else // else-if-chain else comment { - (); - (); - (); + let foo = (); + let bar = (); + let baz = (); } } diff --git a/tests/target/else-if-brace-style-closing-next-line.rs b/tests/target/else-if-brace-style-closing-next-line.rs index c99807dc06d..638f22bb40e 100644 --- a/tests/target/else-if-brace-style-closing-next-line.rs +++ b/tests/target/else-if-brace-style-closing-next-line.rs @@ -2,48 +2,48 @@ fn main() { if false { - (); - (); + let foo = (); + let bar = (); } if false // lone if comment { - (); - (); + let foo = (); + let bar = (); } let a = if 0 > 1 { unreachable!() } else { 0x0 }; if true { - (); + let foo = (); } else if false { - (); - (); + let foo = (); + let bar = (); } else { - (); - (); - (); + let foo = (); + let bar = (); + let baz = (); } if true // else-if-chain if comment { - (); + let foo = (); } else if false // else-if-chain else-if comment { - (); - (); + let foo = (); + let bar = (); } else // else-if-chain else comment { - (); - (); - (); + let foo = (); + let bar = (); + let baz = (); } } diff --git a/tests/target/issue_3868.rs b/tests/target/issue_3868.rs new file mode 100644 index 00000000000..902e2da01fc --- /dev/null +++ b/tests/target/issue_3868.rs @@ -0,0 +1,7 @@ +fn foo() {} + +fn bar() { + for _ in 0..1 {} +} + +fn baz() {}