Skip to content

Commit 31f94e1

Browse files
committed
fix: semicolon_outside_block suggests wrongly when inside macros
1 parent 57cbadd commit 31f94e1

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

clippy_lints/src/semicolon_block.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,11 @@ impl SemicolonBlock {
121121
return;
122122
}
123123

124+
// If the block is from a macro input, we don't want to suggest moving the semicolon
125+
if is_from_macro_input(cx, block) {
126+
return;
127+
}
128+
124129
span_lint_and_then(
125130
cx,
126131
SEMICOLON_OUTSIDE_BLOCK,
@@ -179,3 +184,13 @@ fn get_line(cx: &LateContext<'_>, span: Span) -> Option<usize> {
179184

180185
None
181186
}
187+
188+
fn is_from_macro_input(cx: &LateContext<'_>, block: &Block<'_>) -> bool {
189+
if block.span.from_expansion() {
190+
return false;
191+
}
192+
193+
let this = block.span;
194+
let parent = cx.tcx.hir_span(cx.tcx.parent_hir_id(block.hir_id));
195+
!parent.contains(this) && parent.source_callsite().contains(this)
196+
}

tests/ui/semicolon_outside_block.fixed

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,28 @@ fn main() {
9696

9797
unit_fn_block()
9898
}
99+
100+
fn issue14926() {
101+
macro_rules! gen_code {
102+
[$l:lifetime: $b:block, $b2: block $(,)?] => {
103+
$l: loop {
104+
$b
105+
break $l;
106+
}
107+
$l: loop {
108+
$b2
109+
break $l;
110+
}
111+
};
112+
}
113+
114+
gen_code! {
115+
'root:
116+
{
117+
println!("Block1");
118+
},
119+
{
120+
println!("Block2");
121+
},
122+
}
123+
}

tests/ui/semicolon_outside_block.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,28 @@ fn main() {
9696

9797
unit_fn_block()
9898
}
99+
100+
fn issue14926() {
101+
macro_rules! gen_code {
102+
[$l:lifetime: $b:block, $b2: block $(,)?] => {
103+
$l: loop {
104+
$b
105+
break $l;
106+
}
107+
$l: loop {
108+
$b2
109+
break $l;
110+
}
111+
};
112+
}
113+
114+
gen_code! {
115+
'root:
116+
{
117+
println!("Block1");
118+
},
119+
{
120+
println!("Block2");
121+
},
122+
}
123+
}

0 commit comments

Comments
 (0)