Skip to content

Commit 8fc889c

Browse files
committed
Fix macro statement handling
1 parent dc9bc69 commit 8fc889c

5 files changed

+16
-20
lines changed

clippy_lints/src/semicolon_block.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use clippy_utils::diagnostics::{multispan_sugg_with_applicability, span_lint_and_then};
22
use rustc_errors::Applicability;
33
use rustc_hir::{Block, Expr, ExprKind, Stmt, StmtKind};
4-
use rustc_lint::{LateContext, LateLintPass};
4+
use rustc_lint::{LateContext, LateLintPass, LintContext};
55
use rustc_session::{declare_lint_pass, declare_tool_lint};
66
use rustc_span::Span;
77

@@ -96,7 +96,8 @@ impl LateLintPass<'_> for SemicolonBlock {
9696
}
9797

9898
fn semicolon_inside_block(cx: &LateContext<'_>, block: &Block<'_>, tail: &Expr<'_>, semi_span: Span) {
99-
let insert_span = tail.span.with_lo(tail.span.hi());
99+
let tail_span_end = tail.span.source_callsite().hi();
100+
let insert_span = Span::with_root_ctxt(tail_span_end, tail_span_end);
100101
let remove_span = semi_span.with_lo(block.span.hi());
101102

102103
span_lint_and_then(
@@ -117,6 +118,8 @@ fn semicolon_inside_block(cx: &LateContext<'_>, block: &Block<'_>, tail: &Expr<'
117118

118119
fn semicolon_outside_block(cx: &LateContext<'_>, block: &Block<'_>, tail_stmt_expr: &Expr<'_>, semi_span: Span) {
119120
let insert_span = block.span.with_lo(block.span.hi());
121+
// account for macro calls
122+
let semi_span = cx.sess().source_map().stmt_span(semi_span, block.span);
120123
let remove_span = semi_span.with_lo(tail_stmt_expr.span.source_callsite().hi());
121124

122125
span_lint_and_then(

tests/ui/semicolon_inside_block.fixed

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
macro_rules! m {
1212
(()) => {
13-
();
13+
()
1414
};
1515
(0) => {{
1616
0
@@ -58,7 +58,7 @@ fn main() {
5858
unit_fn_block();
5959
};
6060

61-
{ m!(()) }
61+
{ m!(()); }
6262
{ m!(()); }
6363
{ m!(()); };
6464
m!(0);

tests/ui/semicolon_inside_block.stderr

+2-5
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,8 @@ LL | { m!(()) };
4646
|
4747
help: put the `;` here
4848
|
49-
LL ~ ();
50-
LL | };
51-
...
52-
LL |
53-
LL ~ { m!(()) }
49+
LL - { m!(()) };
50+
LL + { m!(()); }
5451
|
5552

5653
error: aborting due to 4 previous errors

tests/ui/semicolon_outside_block.fixed

+5-9
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,6 @@ macro_rules! m {
2121
(2) => {{
2222
2;
2323
}};
24-
(stmt) => {
25-
stmt;
26-
};
2724
}
2825

2926
fn unit_fn_block() {
@@ -42,8 +39,8 @@ fn main() {
4239
{ unit_fn_block() };
4340
unsafe { unit_fn_block() };
4441

45-
{ unit_fn_block(); }
46-
unsafe { unit_fn_block(); }
42+
{ unit_fn_block() };
43+
unsafe { unit_fn_block() };
4744

4845
{ unit_fn_block(); };
4946
unsafe { unit_fn_block(); };
@@ -54,20 +51,19 @@ fn main() {
5451
};
5552
{
5653
unit_fn_block();
57-
unit_fn_block();
58-
}
54+
unit_fn_block()
55+
};
5956
{
6057
unit_fn_block();
6158
unit_fn_block();
6259
};
6360

6461
{ m!(()) };
65-
{ m!(()); }
62+
{ m!(()) };
6663
{ m!(()); };
6764
m!(0);
6865
m!(1);
6966
m!(2);
70-
{ m!(stmt) };
7167

7268
for _ in [()] {
7369
unit_fn_block();

tests/ui/semicolon_outside_block.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ LL | { m!(()); }
4646
|
4747
help: put the `;` here
4848
|
49-
LL - ()
50-
LL + (); };
49+
LL - { m!(()); }
50+
LL + { m!(()) };
5151
|
5252

5353
error: aborting due to 4 previous errors

0 commit comments

Comments
 (0)