@@ -5,7 +5,7 @@ use clippy_utils::source::snippet_with_macro_callsite;
55use if_chain:: if_chain;
66use rustc_errors:: Applicability ;
77use rustc_hir:: ExprKind ;
8- use rustc_hir:: { Block , StmtKind , ItemKind } ;
8+ use rustc_hir:: { Block , BodyOwnerKind , StmtKind } ;
99use rustc_lint:: { LateContext , LateLintPass } ;
1010use rustc_session:: { declare_lint_pass, declare_tool_lint} ;
1111use 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.
99102fn 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