Skip to content

Commit 595feab

Browse files
committed
Updated semicolon_outside_block lint to fix a bug
1 parent 8a47d20 commit 595feab

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

clippy_lints/src/semicolon_outside_block.rs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use clippy_utils::source::snippet_with_macro_callsite;
55
use if_chain::if_chain;
66
use rustc_errors::Applicability;
77
use rustc_hir::ExprKind;
8-
use rustc_hir::{Block, StmtKind, ItemKind};
8+
use rustc_hir::{Block, BodyOwnerKind, StmtKind};
99
use rustc_lint::{LateContext, LateLintPass};
1010
use rustc_session::{declare_lint_pass, declare_tool_lint};
1111
use rustc_span::BytePos;
@@ -45,15 +45,18 @@ impl LateLintPass<'_> for SemicolonOutsideBlock {
4545
if let StmtKind::Semi(expr) = last.kind;
4646
let t_expr = cx.typeck_results().expr_ty(expr);
4747
if t_expr.is_unit();
48-
49-
// make sure that the block does not belong to a function
50-
let parent_item_id = cx.tcx.hir().get_parent_item(block.hir_id);
51-
let parent_item = cx.tcx.hir().expect_item(parent_item_id);
52-
if let ItemKind::Fn(_, _, body_id) = parent_item.kind;
53-
let item_body = cx.tcx.hir().body(body_id);
54-
if let ExprKind::Block(fn_block, _) = item_body.value.kind;
55-
if fn_block.hir_id != block.hir_id;
5648
then {
49+
// make sure that the block does not belong to a function
50+
for (hir_id, _) in cx.tcx.hir().parent_iter(block.hir_id) {
51+
if_chain! {
52+
if let Some(body_id) = cx.tcx.hir().maybe_body_owned_by(hir_id);
53+
if let BodyOwnerKind::Fn = cx.tcx.hir().body_owner_kind(hir_id);
54+
let item_body = cx.tcx.hir().body(body_id);
55+
if let ExprKind::Block(fn_block, _) = item_body.value.kind;
56+
if fn_block.hir_id == block.hir_id;
57+
then { return }
58+
}
59+
}
5760
// filter out other blocks and the desugared for loop
5861
if let ExprKind::Block(..) | ExprKind::DropTemps(..) = expr.kind { return }
5962

@@ -95,7 +98,7 @@ impl LateLintPass<'_> for SemicolonOutsideBlock {
9598
}
9699
}
97100

98-
/// Takes a span and extzends it until after a semicolon in the last line of the span.
101+
/// Takes a span and extends it until after a semicolon in the last line of the span.
99102
fn expand_span_to_semicolon<'tcx>(cx: &LateContext<'tcx>, expr_span: Span) -> Span {
100103
let expr_span_with_sem = cx.sess().source_map().span_extend_to_next_char(expr_span, ';', false);
101104
expr_span_with_sem.with_hi(expr_span_with_sem.hi().add(BytePos(1)))

0 commit comments

Comments
 (0)