Skip to content

Commit b11827b

Browse files
committed
Fixed error with for and match blocks
1 parent 7e404e3 commit b11827b

File tree

3 files changed

+49
-10
lines changed

3 files changed

+49
-10
lines changed

clippy_lints/src/semicolon_outside_block.rs

+26-5
Original file line numberDiff line numberDiff line change
@@ -49,23 +49,44 @@ impl LateLintPass<'_> for SemicolonOutsideBlock {
4949
// make sure that the block does not belong to a function
5050
for (hir_id, _) in cx.tcx.hir().parent_iter(block.hir_id) {
5151
if let Some(body_id) = cx.tcx.hir().maybe_body_owned_by(hir_id) {
52-
if let BodyOwnerKind::Fn = cx.tcx.hir().body_owner_kind(hir_id) {
52+
if cx.tcx.hir().body_owner_kind(hir_id).is_fn_or_closure() {
5353
let item_body = cx.tcx.hir().body(body_id);
5454
if let ExprKind::Block(fn_block, _) = item_body.value.kind {
55-
if let Some(pot_if) = fn_block.expr {
56-
if let ExprKind::If(..) = pot_if.kind {
55+
for stmt in fn_block.stmts {
56+
if let StmtKind::Expr(pot_ille_expr) = stmt.kind {
57+
if let ExprKind::If(..) |
58+
ExprKind::Loop(..) |
59+
ExprKind::DropTemps(..) |
60+
ExprKind::Match(..) = pot_ille_expr.kind {
61+
return
62+
}
63+
}
64+
}
65+
66+
if let Some(last_expr) = fn_block.expr {
67+
if let ExprKind::If(..) |
68+
ExprKind::Loop(..) |
69+
ExprKind::DropTemps(..) |
70+
ExprKind::Match(..) = last_expr.kind {
5771
return;
5872
}
5973
}
60-
if fn_block.hir_id == block.hir_id {
74+
75+
if fn_block.hir_id == block.hir_id && !matches!(cx.tcx.hir().body_owner_kind(hir_id), BodyOwnerKind::Closure) {
6176
return
6277
}
6378
}
6479
}
6580
}
6681
}
82+
83+
6784
// filter out other blocks and the desugared for loop
68-
if let ExprKind::Block(..) | ExprKind::DropTemps(..) = expr.kind { return }
85+
if let ExprKind::Block(..) |
86+
ExprKind::DropTemps(..) |
87+
ExprKind::If(..) |
88+
ExprKind::Loop(..) |
89+
ExprKind::Match(..) = expr.kind { return }
6990

7091
// make sure we're also having the semicolon at the end of the expression...
7192
let expr_w_sem = expand_span_to_semicolon(cx, expr.span);

tests/ui/semicolon_outside_block.rs

+18
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#![warn(clippy::semicolon_outside_block)]
2+
#![allow(clippy::single_match)]
23

34
unsafe fn f(arg: u32) {}
45

@@ -69,3 +70,20 @@ fn test_if() {
6970
println!("everything alright!");
7071
}
7172
}
73+
74+
fn test_for() {
75+
for project in &[
76+
"clippy_workspace_tests",
77+
"clippy_workspace_tests/src",
78+
"clippy_workspace_tests/subcrate",
79+
"clippy_workspace_tests/subcrate/src",
80+
"clippy_dev",
81+
"clippy_lints",
82+
"clippy_utils",
83+
"rustc_tools_util",
84+
] {
85+
get_unit();
86+
}
87+
88+
get_unit();
89+
}

tests/ui/semicolon_outside_block.stderr

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
error: consider moving the `;` outside the block for consistent formatting
2-
--> $DIR/semicolon_outside_block.rs:9:5
2+
--> $DIR/semicolon_outside_block.rs:10:5
33
|
44
LL | unsafe { f(x); }
55
| ^^^^^^^^^^^^^^^^ help: put the `;` outside the block: `unsafe { f(x) };`
66
|
77
= note: `-D clippy::semicolon-outside-block` implied by `-D warnings`
88

99
error: consider moving the `;` outside the block for consistent formatting
10-
--> $DIR/semicolon_outside_block.rs:15:5
10+
--> $DIR/semicolon_outside_block.rs:16:5
1111
|
1212
LL | / unsafe {
1313
LL | | f(x);
@@ -22,7 +22,7 @@ LL | };
2222
|
2323

2424
error: consider moving the `;` outside the block for consistent formatting
25-
--> $DIR/semicolon_outside_block.rs:23:5
25+
--> $DIR/semicolon_outside_block.rs:24:5
2626
|
2727
LL | / unsafe {
2828
LL | | let _this = 1;
@@ -44,7 +44,7 @@ LL | let _list = 5;
4444
...
4545

4646
error: consider moving the `;` outside the block for consistent formatting
47-
--> $DIR/semicolon_outside_block.rs:46:17
47+
--> $DIR/semicolon_outside_block.rs:47:17
4848
|
4949
LL | let _d = || {
5050
| _________________^
@@ -60,7 +60,7 @@ LL | };
6060
|
6161

6262
error: consider moving the `;` outside the block for consistent formatting
63-
--> $DIR/semicolon_outside_block.rs:53:5
63+
--> $DIR/semicolon_outside_block.rs:54:5
6464
|
6565
LL | / {
6666
LL | | let y = 42;

0 commit comments

Comments
 (0)