@@ -3,6 +3,7 @@ use clippy_utils::source::snippet_with_macro_callsite;
3
3
use clippy_utils:: { get_parent_expr_for_hir, in_macro, spans_on_same_line, sugg} ;
4
4
use if_chain:: if_chain;
5
5
use rustc_errors:: Applicability ;
6
+ use rustc_hir:: Expr ;
6
7
use rustc_hir:: { Block , BlockCheckMode , ExprKind } ;
7
8
use rustc_lint:: { LateContext , LateLintPass } ;
8
9
use rustc_session:: { declare_lint_pass, declare_tool_lint} ;
@@ -45,23 +46,8 @@ impl LateLintPass<'_> for SemicolonIfNothingReturned {
45
46
if t_expr. is_unit( ) ;
46
47
if let snippet = snippet_with_macro_callsite( cx, expr. span, "}" ) ;
47
48
if !snippet. ends_with( '}' ) ;
49
+ if !check_if_inside_block_on_same_line( cx, block, expr) ;
48
50
then {
49
- // check if the block is inside a closure or an unsafe block and don't
50
- // emit if the block is on the same line
51
- if_chain! {
52
- if let Some ( parent) = get_parent_expr_for_hir( cx, block. hir_id) ;
53
-
54
- if !matches!( block. rules, BlockCheckMode :: DefaultBlock ) ||
55
- matches!( parent. kind, ExprKind :: Closure ( ..) | ExprKind :: Block ( ..) ) ;
56
-
57
- if block. stmts. len( ) == 0 ;
58
-
59
- if spans_on_same_line( cx, parent. span, expr. span) ;
60
- then {
61
- return ;
62
- }
63
- }
64
-
65
51
// filter out the desugared `for` loop
66
52
if let ExprKind :: DropTemps ( ..) = & expr. kind {
67
53
return ;
@@ -82,3 +68,23 @@ impl LateLintPass<'_> for SemicolonIfNothingReturned {
82
68
}
83
69
}
84
70
}
71
+
72
+ /// Check if this block is inside a closure or an unsafe block or a normal on the same line.
73
+ fn check_if_inside_block_on_same_line < ' tcx > (
74
+ cx : & LateContext < ' tcx > ,
75
+ block : & ' tcx Block < ' tcx > ,
76
+ last_expr : & ' tcx Expr < ' _ > ,
77
+ ) -> bool {
78
+ if_chain ! {
79
+ if let Some ( parent) = get_parent_expr_for_hir( cx, block. hir_id) ;
80
+
81
+ if !matches!( block. rules, BlockCheckMode :: DefaultBlock ) ||
82
+ matches!( parent. kind, ExprKind :: Closure ( ..) | ExprKind :: Block ( ..) ) ;
83
+
84
+ if block. stmts. is_empty( ) ;
85
+ then {
86
+ return spans_on_same_line( cx, parent. span, last_expr. span) ;
87
+ }
88
+ }
89
+ false
90
+ }
0 commit comments