@@ -5,7 +5,7 @@ use clippy_utils::source::snippet_with_macro_callsite;
5
5
use if_chain:: if_chain;
6
6
use rustc_errors:: Applicability ;
7
7
use rustc_hir:: ExprKind ;
8
- use rustc_hir:: { Block , StmtKind , ItemKind } ;
8
+ use rustc_hir:: { Block , BodyOwnerKind , StmtKind } ;
9
9
use rustc_lint:: { LateContext , LateLintPass } ;
10
10
use rustc_session:: { declare_lint_pass, declare_tool_lint} ;
11
11
use rustc_span:: BytePos ;
@@ -45,15 +45,18 @@ impl LateLintPass<'_> for SemicolonOutsideBlock {
45
45
if let StmtKind :: Semi ( expr) = last. kind;
46
46
let t_expr = cx. typeck_results( ) . expr_ty( expr) ;
47
47
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;
56
48
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
+ }
57
60
// filter out other blocks and the desugared for loop
58
61
if let ExprKind :: Block ( ..) | ExprKind :: DropTemps ( ..) = expr. kind { return }
59
62
@@ -95,7 +98,7 @@ impl LateLintPass<'_> for SemicolonOutsideBlock {
95
98
}
96
99
}
97
100
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.
99
102
fn expand_span_to_semicolon < ' tcx > ( cx : & LateContext < ' tcx > , expr_span : Span ) -> Span {
100
103
let expr_span_with_sem = cx. sess ( ) . source_map ( ) . span_extend_to_next_char ( expr_span, ';' , false ) ;
101
104
expr_span_with_sem. with_hi ( expr_span_with_sem. hi ( ) . add ( BytePos ( 1 ) ) )
0 commit comments