Skip to content

Commit

Permalink
fix: checking block emptiness (#3878)
Browse files Browse the repository at this point in the history
  • Loading branch information
calebcartwright authored and topecongiro committed Oct 26, 2019
1 parent 4967523 commit ed7c032
Show file tree
Hide file tree
Showing 13 changed files with 157 additions and 124 deletions.
17 changes: 15 additions & 2 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ fn rewrite_empty_block(
prefix: &str,
shape: Shape,
) -> Option<String> {
if !block.stmts.is_empty() {
if block_has_statements(&block) {
return None;
}

Expand Down Expand Up @@ -1165,14 +1165,27 @@ 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(
context: &RewriteContext<'_>,
block: &ast::Block,
attrs: Option<&[ast::Attribute]>,
) -> bool {
block.stmts.is_empty()
!block_has_statements(&block)
&& !block_contains_comment(context, block)
&& attrs.map_or(true, |a| inner_attributes(a).is_empty())
}
Expand Down
14 changes: 7 additions & 7 deletions tests/source/control-brace-style-always-next-line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,33 @@

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 = ();
}


for obj in iter {
for sub_obj in obj
{
'nested_while_label: while cond {
();
let foo = ();
}
}
}
Expand Down
14 changes: 7 additions & 7 deletions tests/source/control-brace-style-always-same-line.rs
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
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 = ();
}


for obj in iter {
for sub_obj in obj
{
'nested_while_label: while cond {
();
let foo = ();
}
}
}
Expand Down
32 changes: 16 additions & 16 deletions tests/source/else-if-brace-style-always-next-line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
fn main() {
if false
{
();
();
let foo = ();
let bar = ();
}

if false // lone if comment
{
();
();
let foo = ();
let bar = ();
}


Expand All @@ -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 = ();
}
}
32 changes: 16 additions & 16 deletions tests/source/else-if-brace-style-always-same-line.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
fn main() {
if false
{
();
();
let foo = ();
let bar = ();
}

if false // lone if comment
{
();
();
let foo = ();
let bar = ();
}


Expand All @@ -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 = ();
}
}
32 changes: 16 additions & 16 deletions tests/source/else-if-brace-style-closing-next-line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
fn main() {
if false
{
();
();
let foo = ();
let bar = ();
}

if false // lone if comment
{
();
();
let foo = ();
let bar = ();
}


Expand All @@ -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 = ();
}
}
13 changes: 13 additions & 0 deletions tests/source/issue_3868.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
fn foo() {
;
}

fn bar() {
for _ in 0..1 {
;
}
}

fn baz() {
();
}
12 changes: 6 additions & 6 deletions tests/target/control-brace-style-always-next-line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -31,7 +31,7 @@ fn main() {
{
'nested_while_label: while cond
{
();
let foo = ();
}
}
}
Expand Down
12 changes: 6 additions & 6 deletions tests/target/control-brace-style-always-same-line.rs
Original file line number Diff line number Diff line change
@@ -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 = ();
}
}
}
Expand Down
Loading

0 comments on commit ed7c032

Please sign in to comment.