Skip to content

Commit 611aee9

Browse files
committed
Fixed failing tests
1 parent 30d76f9 commit 611aee9

File tree

1 file changed

+22
-16
lines changed

1 file changed

+22
-16
lines changed

clippy_lints/src/semicolon_if_nothing_returned.rs

+22-16
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use clippy_utils::source::snippet_with_macro_callsite;
33
use clippy_utils::{get_parent_expr_for_hir, in_macro, spans_on_same_line, sugg};
44
use if_chain::if_chain;
55
use rustc_errors::Applicability;
6+
use rustc_hir::Expr;
67
use rustc_hir::{Block, BlockCheckMode, ExprKind};
78
use rustc_lint::{LateContext, LateLintPass};
89
use rustc_session::{declare_lint_pass, declare_tool_lint};
@@ -45,23 +46,8 @@ impl LateLintPass<'_> for SemicolonIfNothingReturned {
4546
if t_expr.is_unit();
4647
if let snippet = snippet_with_macro_callsite(cx, expr.span, "}");
4748
if !snippet.ends_with('}');
49+
if !check_if_inside_block_on_same_line(cx, block, expr);
4850
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-
6551
// filter out the desugared `for` loop
6652
if let ExprKind::DropTemps(..) = &expr.kind {
6753
return;
@@ -82,3 +68,23 @@ impl LateLintPass<'_> for SemicolonIfNothingReturned {
8268
}
8369
}
8470
}
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

Comments
 (0)