Skip to content

Commit

Permalink
Fixed error with for and match blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
1c3t3a committed Aug 13, 2021
1 parent 7e404e3 commit b11827b
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 10 deletions.
31 changes: 26 additions & 5 deletions clippy_lints/src/semicolon_outside_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,23 +49,44 @@ impl LateLintPass<'_> for SemicolonOutsideBlock {
// make sure that the block does not belong to a function
for (hir_id, _) in cx.tcx.hir().parent_iter(block.hir_id) {
if let Some(body_id) = cx.tcx.hir().maybe_body_owned_by(hir_id) {
if let BodyOwnerKind::Fn = cx.tcx.hir().body_owner_kind(hir_id) {
if cx.tcx.hir().body_owner_kind(hir_id).is_fn_or_closure() {
let item_body = cx.tcx.hir().body(body_id);
if let ExprKind::Block(fn_block, _) = item_body.value.kind {
if let Some(pot_if) = fn_block.expr {
if let ExprKind::If(..) = pot_if.kind {
for stmt in fn_block.stmts {
if let StmtKind::Expr(pot_ille_expr) = stmt.kind {
if let ExprKind::If(..) |
ExprKind::Loop(..) |
ExprKind::DropTemps(..) |
ExprKind::Match(..) = pot_ille_expr.kind {
return
}
}
}

if let Some(last_expr) = fn_block.expr {
if let ExprKind::If(..) |
ExprKind::Loop(..) |
ExprKind::DropTemps(..) |
ExprKind::Match(..) = last_expr.kind {
return;
}
}
if fn_block.hir_id == block.hir_id {

if fn_block.hir_id == block.hir_id && !matches!(cx.tcx.hir().body_owner_kind(hir_id), BodyOwnerKind::Closure) {
return
}
}
}
}
}


// filter out other blocks and the desugared for loop
if let ExprKind::Block(..) | ExprKind::DropTemps(..) = expr.kind { return }
if let ExprKind::Block(..) |
ExprKind::DropTemps(..) |
ExprKind::If(..) |
ExprKind::Loop(..) |
ExprKind::Match(..) = expr.kind { return }

// make sure we're also having the semicolon at the end of the expression...
let expr_w_sem = expand_span_to_semicolon(cx, expr.span);
Expand Down
18 changes: 18 additions & 0 deletions tests/ui/semicolon_outside_block.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#![warn(clippy::semicolon_outside_block)]
#![allow(clippy::single_match)]

unsafe fn f(arg: u32) {}

Expand Down Expand Up @@ -69,3 +70,20 @@ fn test_if() {
println!("everything alright!");
}
}

fn test_for() {
for project in &[
"clippy_workspace_tests",
"clippy_workspace_tests/src",
"clippy_workspace_tests/subcrate",
"clippy_workspace_tests/subcrate/src",
"clippy_dev",
"clippy_lints",
"clippy_utils",
"rustc_tools_util",
] {
get_unit();
}

get_unit();
}
10 changes: 5 additions & 5 deletions tests/ui/semicolon_outside_block.stderr
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
error: consider moving the `;` outside the block for consistent formatting
--> $DIR/semicolon_outside_block.rs:9:5
--> $DIR/semicolon_outside_block.rs:10:5
|
LL | unsafe { f(x); }
| ^^^^^^^^^^^^^^^^ help: put the `;` outside the block: `unsafe { f(x) };`
|
= note: `-D clippy::semicolon-outside-block` implied by `-D warnings`

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

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

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

error: consider moving the `;` outside the block for consistent formatting
--> $DIR/semicolon_outside_block.rs:53:5
--> $DIR/semicolon_outside_block.rs:54:5
|
LL | / {
LL | | let y = 42;
Expand Down

0 comments on commit b11827b

Please sign in to comment.