Skip to content

Commit cf93045

Browse files
committed
turn into early-pass lint
- replace the check for trailing expr / last stmt with a check on last `block.stmts.last()` this by itself fixes the issue
1 parent ef51f19 commit cf93045

File tree

5 files changed

+20
-65
lines changed

5 files changed

+20
-65
lines changed

clippy_lints/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -733,7 +733,7 @@ pub fn register_lint_passes(store: &mut rustc_lint::LintStore, conf: &'static Co
733733
store.register_late_pass(|_| Box::new(from_raw_with_void_ptr::FromRawWithVoidPtr));
734734
store.register_late_pass(|_| Box::new(suspicious_xor_used_as_pow::ConfusingXorAndPow));
735735
store.register_late_pass(move |_| Box::new(manual_is_ascii_check::ManualIsAsciiCheck::new(conf)));
736-
store.register_late_pass(move |_| Box::new(semicolon_block::SemicolonBlock::new(conf)));
736+
store.register_early_pass(move || Box::new(semicolon_block::SemicolonBlock::new(conf)));
737737
store.register_late_pass(|_| Box::new(permissions_set_readonly_false::PermissionsSetReadonlyFalse));
738738
store.register_late_pass(|_| Box::new(size_of_ref::SizeOfRef));
739739
store.register_late_pass(|_| Box::new(multiple_unsafe_ops_per_block::MultipleUnsafeOpsPerBlock));

clippy_lints/src/semicolon_block.rs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use clippy_config::Conf;
22
use clippy_utils::diagnostics::span_lint_and_then;
3+
use rustc_ast::{Block, Expr, ExprKind, Stmt, StmtKind};
34
use rustc_errors::Applicability;
4-
use rustc_hir::{Block, Expr, ExprKind, Stmt, StmtKind};
5-
use rustc_lint::{LateContext, LateLintPass, LintContext};
5+
use rustc_lint::{EarlyContext, EarlyLintPass, LintContext};
66
use rustc_session::impl_lint_pass;
77
use rustc_span::Span;
88

@@ -78,7 +78,7 @@ impl SemicolonBlock {
7878
}
7979
}
8080

81-
fn semicolon_inside_block(&self, cx: &impl LintContext, block: &Block<'_>, tail: &Expr<'_>, semi_span: Span) {
81+
fn semicolon_inside_block(&self, cx: &impl LintContext, block: &Block, tail: &Expr, semi_span: Span) {
8282
let insert_span = tail.span.source_callsite().shrink_to_hi();
8383
let remove_span = semi_span.with_lo(block.span.hi());
8484

@@ -101,7 +101,7 @@ impl SemicolonBlock {
101101
);
102102
}
103103

104-
fn semicolon_outside_block(&self, cx: &impl LintContext, block: &Block<'_>, tail_stmt_expr: &Expr<'_>) {
104+
fn semicolon_outside_block(&self, cx: &impl LintContext, block: &Block, tail_stmt_expr: &Expr) {
105105
let insert_span = block.span.shrink_to_hi();
106106

107107
// For macro call semicolon statements (`mac!();`), the statement's span does not actually
@@ -137,26 +137,27 @@ impl SemicolonBlock {
137137
}
138138
}
139139

140-
impl LateLintPass<'_> for SemicolonBlock {
141-
fn check_stmt(&mut self, cx: &LateContext<'_>, stmt: &Stmt<'_>) {
142-
match stmt.kind {
140+
impl EarlyLintPass for SemicolonBlock {
141+
fn check_stmt(&mut self, cx: &EarlyContext<'_>, stmt: &Stmt) {
142+
match &stmt.kind {
143143
StmtKind::Expr(expr)
144-
if let ExprKind::Block(block, _) = expr.kind
144+
if let ExprKind::Block(block, _) = &expr.kind
145145
&& !block.span.from_expansion()
146146
&& stmt.span.contains(block.span) =>
147147
{
148-
if block.expr.is_none()
149-
&& let [.., stmt] = block.stmts
150-
&& let StmtKind::Semi(expr) = stmt.kind
148+
if let Some(stmt) = block.stmts.last()
149+
&& let StmtKind::Semi(expr) = &stmt.kind
151150
{
152151
self.semicolon_outside_block(cx, block, expr);
153152
}
154153
},
155154
StmtKind::Semi(expr)
156-
if let ExprKind::Block(block, _) = expr.kind
155+
if let ExprKind::Block(block, _) = &expr.kind
157156
&& !block.span.from_expansion() =>
158157
{
159-
if let Some(tail) = block.expr {
158+
if let Some(expr) = block.stmts.last()
159+
&& let StmtKind::Expr(tail) = &expr.kind
160+
{
160161
self.semicolon_inside_block(cx, block, tail, stmt.span);
161162
}
162163
},

tests/ui/semicolon_inside_block.fixed

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,16 +90,13 @@ fn main() {
9090

9191
fn issue15380() {
9292
#[rustfmt::skip]
93-
( {0;0;}
94-
//~^ semicolon_inside_block
93+
( {0;0});
9594

9695
({
97-
//~^ semicolon_inside_block
98-
0;
9996
0;
100-
}
97+
0
98+
});
10199

102100
#[rustfmt::skip]
103-
(({0;}
104-
//~^ semicolon_inside_block
101+
(({0})) ;
105102
}

tests/ui/semicolon_inside_block.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,15 +91,12 @@ fn main() {
9191
fn issue15380() {
9292
#[rustfmt::skip]
9393
( {0;0});
94-
//~^ semicolon_inside_block
9594

9695
({
97-
//~^ semicolon_inside_block
9896
0;
9997
0
10098
});
10199

102100
#[rustfmt::skip]
103101
(({0})) ;
104-
//~^ semicolon_inside_block
105102
}

tests/ui/semicolon_inside_block.stderr

Lines changed: 1 addition & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -52,45 +52,5 @@ LL - { m!(()) };
5252
LL + { m!(()); }
5353
|
5454

55-
error: consider moving the `;` inside the block for consistent formatting
56-
--> tests/ui/semicolon_inside_block.rs:93:5
57-
|
58-
LL | ( {0;0});
59-
| ^^^^^^^^^
60-
|
61-
help: put the `;` here
62-
|
63-
LL - ( {0;0});
64-
LL + ( {0;0;}
65-
|
66-
67-
error: consider moving the `;` inside the block for consistent formatting
68-
--> tests/ui/semicolon_inside_block.rs:96:5
69-
|
70-
LL | / ({
71-
LL | |
72-
LL | | 0;
73-
LL | | 0
74-
LL | | });
75-
| |_______^
76-
|
77-
help: put the `;` here
78-
|
79-
LL ~ 0;
80-
LL ~ }
81-
|
82-
83-
error: consider moving the `;` inside the block for consistent formatting
84-
--> tests/ui/semicolon_inside_block.rs:103:5
85-
|
86-
LL | (({0})) ;
87-
| ^^^^^^^^^^^^
88-
|
89-
help: put the `;` here
90-
|
91-
LL - (({0})) ;
92-
LL + (({0;}
93-
|
94-
95-
error: aborting due to 7 previous errors
55+
error: aborting due to 4 previous errors
9656

0 commit comments

Comments
 (0)