Skip to content

Commit ed7c032

Browse files
calebcartwrighttopecongiro
authored andcommitted
fix: checking block emptiness (#3878)
1 parent 4967523 commit ed7c032

13 files changed

+157
-124
lines changed

src/expr.rs

+15-2
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ fn rewrite_empty_block(
421421
prefix: &str,
422422
shape: Shape,
423423
) -> Option<String> {
424-
if !block.stmts.is_empty() {
424+
if block_has_statements(&block) {
425425
return None;
426426
}
427427

@@ -1165,14 +1165,27 @@ pub(crate) fn is_simple_block_stmt(
11651165
&& attrs.map_or(true, |a| a.is_empty())
11661166
}
11671167

1168+
fn block_has_statements(block: &ast::Block) -> bool {
1169+
block.stmts.iter().any(|stmt| {
1170+
if let ast::StmtKind::Semi(ref expr) = stmt.kind {
1171+
if let ast::ExprKind::Tup(ref tup_exprs) = expr.kind {
1172+
if tup_exprs.is_empty() {
1173+
return false;
1174+
}
1175+
}
1176+
}
1177+
true
1178+
})
1179+
}
1180+
11681181
/// Checks whether a block contains no statements, expressions, comments, or
11691182
/// inner attributes.
11701183
pub(crate) fn is_empty_block(
11711184
context: &RewriteContext<'_>,
11721185
block: &ast::Block,
11731186
attrs: Option<&[ast::Attribute]>,
11741187
) -> bool {
1175-
block.stmts.is_empty()
1188+
!block_has_statements(&block)
11761189
&& !block_contains_comment(context, block)
11771190
&& attrs.map_or(true, |a| inner_attributes(a).is_empty())
11781191
}

tests/source/control-brace-style-always-next-line.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,33 @@
22

33
fn main() {
44
loop {
5-
();
6-
();
5+
let foo = ();
6+
let bar = ();
77
}
88

99

10-
'label: loop // loop comment
10+
'label: loop // loop comment
1111
{
12-
();
12+
let foo = ();
1313
}
1414

1515

1616
cond = true;
1717
while cond {
18-
();
18+
let foo = ();
1919
}
2020

2121

2222
'while_label: while cond { // while comment
23-
();
23+
let foo = ();
2424
}
2525

2626

2727
for obj in iter {
2828
for sub_obj in obj
2929
{
3030
'nested_while_label: while cond {
31-
();
31+
let foo = ();
3232
}
3333
}
3434
}

tests/source/control-brace-style-always-same-line.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,32 @@
11
fn main() {
22
loop {
3-
();
4-
();
3+
let foo = ();
4+
let bar = ();
55
}
66

77

8-
'label: loop // loop comment
8+
'label: loop // loop comment
99
{
10-
();
10+
let foo = ();
1111
}
1212

1313

1414
cond = true;
1515
while cond {
16-
();
16+
let foo = ();
1717
}
1818

1919

2020
'while_label: while cond { // while comment
21-
();
21+
let foo = ();
2222
}
2323

2424

2525
for obj in iter {
2626
for sub_obj in obj
2727
{
2828
'nested_while_label: while cond {
29-
();
29+
let foo = ();
3030
}
3131
}
3232
}

tests/source/else-if-brace-style-always-next-line.rs

+16-16
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
fn main() {
44
if false
55
{
6-
();
7-
();
6+
let foo = ();
7+
let bar = ();
88
}
99

1010
if false // lone if comment
1111
{
12-
();
13-
();
12+
let foo = ();
13+
let bar = ();
1414
}
1515

1616

@@ -26,29 +26,29 @@ fn main() {
2626

2727
if true
2828
{
29-
();
29+
let foo = ();
3030
} else if false {
31-
();
32-
();
31+
let foo = ();
32+
let bar = ();
3333
}
3434
else {
35-
();
36-
();
37-
();
35+
let foo = ();
36+
let bar = ();
37+
let baz = ();
3838
}
3939

4040
if true // else-if-chain if comment
4141
{
42-
();
42+
let foo = ();
4343
}
4444
else if false // else-if-chain else-if comment
4545
{
46-
();
47-
();
46+
let foo = ();
47+
let bar = ();
4848
} else // else-if-chain else comment
4949
{
50-
();
51-
();
52-
();
50+
let foo = ();
51+
let bar = ();
52+
let baz = ();
5353
}
5454
}
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
fn main() {
22
if false
33
{
4-
();
5-
();
4+
let foo = ();
5+
let bar = ();
66
}
77

88
if false // lone if comment
99
{
10-
();
11-
();
10+
let foo = ();
11+
let bar = ();
1212
}
1313

1414

@@ -24,29 +24,29 @@ fn main() {
2424

2525
if true
2626
{
27-
();
27+
let foo = ();
2828
} else if false {
29-
();
30-
();
29+
let foo = ();
30+
let bar = ();
3131
}
3232
else {
33-
();
34-
();
35-
();
33+
let foo = ();
34+
let bar = ();
35+
let baz = ();
3636
}
3737

3838
if true // else-if-chain if comment
3939
{
40-
();
40+
let foo = ();
4141
}
4242
else if false // else-if-chain else-if comment
4343
{
44-
();
45-
();
44+
let foo = ();
45+
let bar = ();
4646
} else // else-if-chain else comment
4747
{
48-
();
49-
();
50-
();
48+
let foo = ();
49+
let bar = ();
50+
let baz = ();
5151
}
5252
}

tests/source/else-if-brace-style-closing-next-line.rs

+16-16
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
fn main() {
44
if false
55
{
6-
();
7-
();
6+
let foo = ();
7+
let bar = ();
88
}
99

1010
if false // lone if comment
1111
{
12-
();
13-
();
12+
let foo = ();
13+
let bar = ();
1414
}
1515

1616

@@ -26,29 +26,29 @@ fn main() {
2626

2727
if true
2828
{
29-
();
29+
let foo = ();
3030
} else if false {
31-
();
32-
();
31+
let foo = ();
32+
let bar = ();
3333
}
3434
else {
35-
();
36-
();
37-
();
35+
let foo = ();
36+
let bar = ();
37+
let baz = ();
3838
}
3939

4040
if true // else-if-chain if comment
4141
{
42-
();
42+
let foo = ();
4343
}
4444
else if false // else-if-chain else-if comment
4545
{
46-
();
47-
();
46+
let foo = ();
47+
let bar = ();
4848
} else // else-if-chain else comment
4949
{
50-
();
51-
();
52-
();
50+
let foo = ();
51+
let bar = ();
52+
let baz = ();
5353
}
5454
}

tests/source/issue_3868.rs

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
fn foo() {
2+
;
3+
}
4+
5+
fn bar() {
6+
for _ in 0..1 {
7+
;
8+
}
9+
}
10+
11+
fn baz() {
12+
();
13+
}

tests/target/control-brace-style-always-next-line.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,26 @@
33
fn main() {
44
loop
55
{
6-
();
7-
();
6+
let foo = ();
7+
let bar = ();
88
}
99

1010
'label: loop
1111
// loop comment
1212
{
13-
();
13+
let foo = ();
1414
}
1515

1616
cond = true;
1717
while cond
1818
{
19-
();
19+
let foo = ();
2020
}
2121

2222
'while_label: while cond
2323
{
2424
// while comment
25-
();
25+
let foo = ();
2626
}
2727

2828
for obj in iter
@@ -31,7 +31,7 @@ fn main() {
3131
{
3232
'nested_while_label: while cond
3333
{
34-
();
34+
let foo = ();
3535
}
3636
}
3737
}

tests/target/control-brace-style-always-same-line.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
11
fn main() {
22
loop {
3-
();
4-
();
3+
let foo = ();
4+
let bar = ();
55
}
66

77
'label: loop
88
// loop comment
99
{
10-
();
10+
let foo = ();
1111
}
1212

1313
cond = true;
1414
while cond {
15-
();
15+
let foo = ();
1616
}
1717

1818
'while_label: while cond {
1919
// while comment
20-
();
20+
let foo = ();
2121
}
2222

2323
for obj in iter {
2424
for sub_obj in obj {
2525
'nested_while_label: while cond {
26-
();
26+
let foo = ();
2727
}
2828
}
2929
}

0 commit comments

Comments
 (0)