Fix macro statement handling

This commit is contained in:
Lukas Wirth 2022-11-16 14:30:53 +01:00
parent 23744cd4ba
commit dd1163f461
5 changed files with 16 additions and 20 deletions

View file

@ -1,7 +1,7 @@
use clippy_utils::diagnostics::{multispan_sugg_with_applicability, span_lint_and_then};
use rustc_errors::Applicability;
use rustc_hir::{Block, Expr, ExprKind, Stmt, StmtKind};
use rustc_lint::{LateContext, LateLintPass};
use rustc_lint::{LateContext, LateLintPass, LintContext};
use rustc_session::{declare_lint_pass, declare_tool_lint};
use rustc_span::Span;
@ -96,7 +96,8 @@ impl LateLintPass<'_> for SemicolonBlock {
}
fn semicolon_inside_block(cx: &LateContext<'_>, block: &Block<'_>, tail: &Expr<'_>, semi_span: Span) {
let insert_span = tail.span.with_lo(tail.span.hi());
let tail_span_end = tail.span.source_callsite().hi();
let insert_span = Span::with_root_ctxt(tail_span_end, tail_span_end);
let remove_span = semi_span.with_lo(block.span.hi());
span_lint_and_then(
@ -117,6 +118,8 @@ fn semicolon_inside_block(cx: &LateContext<'_>, block: &Block<'_>, tail: &Expr<'
fn semicolon_outside_block(cx: &LateContext<'_>, block: &Block<'_>, tail_stmt_expr: &Expr<'_>, semi_span: Span) {
let insert_span = block.span.with_lo(block.span.hi());
// account for macro calls
let semi_span = cx.sess().source_map().stmt_span(semi_span, block.span);
let remove_span = semi_span.with_lo(tail_stmt_expr.span.source_callsite().hi());
span_lint_and_then(

View file

@ -10,7 +10,7 @@
macro_rules! m {
(()) => {
();
()
};
(0) => {{
0
@ -58,7 +58,7 @@ fn main() {
unit_fn_block();
};
{ m!(()) }
{ m!(()); }
{ m!(()); }
{ m!(()); };
m!(0);

View file

@ -46,11 +46,8 @@ LL | { m!(()) };
|
help: put the `;` here
|
LL ~ ();
LL | };
...
LL |
LL ~ { m!(()) }
LL - { m!(()) };
LL + { m!(()); }
|
error: aborting due to 4 previous errors

View file

@ -21,9 +21,6 @@ macro_rules! m {
(2) => {{
2;
}};
(stmt) => {
stmt;
};
}
fn unit_fn_block() {
@ -42,8 +39,8 @@ fn main() {
{ unit_fn_block() };
unsafe { unit_fn_block() };
{ unit_fn_block(); }
unsafe { unit_fn_block(); }
{ unit_fn_block() };
unsafe { unit_fn_block() };
{ unit_fn_block(); };
unsafe { unit_fn_block(); };
@ -54,20 +51,19 @@ fn main() {
};
{
unit_fn_block();
unit_fn_block();
}
unit_fn_block()
};
{
unit_fn_block();
unit_fn_block();
};
{ m!(()) };
{ m!(()); }
{ m!(()) };
{ m!(()); };
m!(0);
m!(1);
m!(2);
{ m!(stmt) };
for _ in [()] {
unit_fn_block();

View file

@ -46,8 +46,8 @@ LL | { m!(()); }
|
help: put the `;` here
|
LL - ()
LL + (); };
LL - { m!(()); }
LL + { m!(()) };
|
error: aborting due to 4 previous errors