mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-27 23:20:39 +00:00
Use multi-span suggestions
This commit is contained in:
parent
ba951e3ca7
commit
23744cd4ba
5 changed files with 126 additions and 86 deletions
|
@ -1,10 +1,9 @@
|
||||||
use clippy_utils::diagnostics::span_lint_and_sugg;
|
use clippy_utils::diagnostics::{multispan_sugg_with_applicability, span_lint_and_then};
|
||||||
use clippy_utils::source::snippet_with_macro_callsite;
|
|
||||||
use clippy_utils::{get_parent_expr_for_hir, get_parent_node};
|
|
||||||
use rustc_errors::Applicability;
|
use rustc_errors::Applicability;
|
||||||
use rustc_hir::{Block, Expr, ExprKind, Node, Stmt, StmtKind};
|
use rustc_hir::{Block, Expr, ExprKind, Stmt, StmtKind};
|
||||||
use rustc_lint::{LateContext, LateLintPass};
|
use rustc_lint::{LateContext, LateLintPass};
|
||||||
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
||||||
|
use rustc_span::Span;
|
||||||
|
|
||||||
declare_clippy_lint! {
|
declare_clippy_lint! {
|
||||||
/// ### What it does
|
/// ### What it does
|
||||||
|
@ -65,69 +64,73 @@ declare_clippy_lint! {
|
||||||
declare_lint_pass!(SemicolonBlock => [SEMICOLON_INSIDE_BLOCK, SEMICOLON_OUTSIDE_BLOCK]);
|
declare_lint_pass!(SemicolonBlock => [SEMICOLON_INSIDE_BLOCK, SEMICOLON_OUTSIDE_BLOCK]);
|
||||||
|
|
||||||
impl LateLintPass<'_> for SemicolonBlock {
|
impl LateLintPass<'_> for SemicolonBlock {
|
||||||
fn check_block(&mut self, cx: &LateContext<'_>, block: &Block<'_>) {
|
fn check_stmt(&mut self, cx: &LateContext<'_>, stmt: &Stmt<'_>) {
|
||||||
semicolon_inside_block(cx, block);
|
match stmt.kind {
|
||||||
semicolon_outside_block(cx, block);
|
StmtKind::Expr(Expr {
|
||||||
|
kind:
|
||||||
|
ExprKind::Block(
|
||||||
|
block @ Block {
|
||||||
|
expr: None,
|
||||||
|
stmts:
|
||||||
|
&[
|
||||||
|
..,
|
||||||
|
Stmt {
|
||||||
|
kind: StmtKind::Semi(expr),
|
||||||
|
span,
|
||||||
|
..
|
||||||
|
},
|
||||||
|
],
|
||||||
|
..
|
||||||
|
},
|
||||||
|
_,
|
||||||
|
),
|
||||||
|
..
|
||||||
|
}) if !block.span.from_expansion() => semicolon_outside_block(cx, block, expr, span),
|
||||||
|
StmtKind::Semi(Expr {
|
||||||
|
kind: ExprKind::Block(block @ Block { expr: Some(tail), .. }, _),
|
||||||
|
..
|
||||||
|
}) if !block.span.from_expansion() => semicolon_inside_block(cx, block, tail, stmt.span),
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn semicolon_inside_block(cx: &LateContext<'_>, block: &Block<'_>) {
|
fn semicolon_inside_block(cx: &LateContext<'_>, block: &Block<'_>, tail: &Expr<'_>, semi_span: Span) {
|
||||||
if !block.span.from_expansion()
|
let insert_span = tail.span.with_lo(tail.span.hi());
|
||||||
&& let Some(tail) = block.expr
|
let remove_span = semi_span.with_lo(block.span.hi());
|
||||||
&& let Some(block_expr @ Expr { kind: ExprKind::Block(_, _), ..}) = get_parent_expr_for_hir(cx, block.hir_id)
|
|
||||||
&& let Some(Node::Stmt(Stmt { kind: StmtKind::Semi(_), span, .. })) = get_parent_node(cx.tcx, block_expr.hir_id)
|
|
||||||
{
|
|
||||||
let expr_snip = snippet_with_macro_callsite(cx, tail.span, "..");
|
|
||||||
|
|
||||||
let mut suggestion: String = snippet_with_macro_callsite(cx, block.span, "..").to_string();
|
span_lint_and_then(
|
||||||
|
|
||||||
if let Some((expr_offset, _)) = suggestion.rmatch_indices(&*expr_snip).next() {
|
|
||||||
suggestion.insert(expr_offset + expr_snip.len(), ';');
|
|
||||||
} else {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
span_lint_and_sugg(
|
|
||||||
cx,
|
cx,
|
||||||
SEMICOLON_INSIDE_BLOCK,
|
SEMICOLON_INSIDE_BLOCK,
|
||||||
*span,
|
semi_span,
|
||||||
"consider moving the `;` inside the block for consistent formatting",
|
"consider moving the `;` inside the block for consistent formatting",
|
||||||
|
|diag| {
|
||||||
|
multispan_sugg_with_applicability(
|
||||||
|
diag,
|
||||||
"put the `;` here",
|
"put the `;` here",
|
||||||
suggestion,
|
Applicability::MachineApplicable,
|
||||||
Applicability::MaybeIncorrect,
|
[(remove_span, String::new()), (insert_span, ";".to_owned())],
|
||||||
|
);
|
||||||
|
},
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
fn semicolon_outside_block(cx: &LateContext<'_>, block: &Block<'_>) {
|
fn semicolon_outside_block(cx: &LateContext<'_>, block: &Block<'_>, tail_stmt_expr: &Expr<'_>, semi_span: Span) {
|
||||||
if !block.span.from_expansion()
|
let insert_span = block.span.with_lo(block.span.hi());
|
||||||
&& block.expr.is_none()
|
let remove_span = semi_span.with_lo(tail_stmt_expr.span.source_callsite().hi());
|
||||||
&& let [.., Stmt { kind: StmtKind::Semi(expr), .. }] = block.stmts
|
|
||||||
&& let Some(block_expr @ Expr { kind: ExprKind::Block(_, _), ..}) = get_parent_expr_for_hir(cx,block.hir_id)
|
|
||||||
&& let Some(Node::Stmt(Stmt { kind: StmtKind::Expr(_), .. })) = get_parent_node(cx.tcx, block_expr.hir_id)
|
|
||||||
{
|
|
||||||
let expr_snip = snippet_with_macro_callsite(cx, expr.span, "..");
|
|
||||||
|
|
||||||
let mut suggestion: String = snippet_with_macro_callsite(cx, block.span, "..").to_string();
|
span_lint_and_then(
|
||||||
|
|
||||||
if let Some((expr_offset, _)) = suggestion.rmatch_indices(&*expr_snip).next()
|
|
||||||
&& let Some(semi_offset) = suggestion[expr_offset + expr_snip.len()..].find(';')
|
|
||||||
{
|
|
||||||
suggestion.remove(expr_offset + expr_snip.len() + semi_offset);
|
|
||||||
} else {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
suggestion.push(';');
|
|
||||||
|
|
||||||
span_lint_and_sugg(
|
|
||||||
cx,
|
cx,
|
||||||
SEMICOLON_OUTSIDE_BLOCK,
|
SEMICOLON_OUTSIDE_BLOCK,
|
||||||
block.span,
|
block.span,
|
||||||
"consider moving the `;` outside the block for consistent formatting",
|
"consider moving the `;` outside the block for consistent formatting",
|
||||||
"put the `;` outside the block",
|
|diag| {
|
||||||
suggestion,
|
multispan_sugg_with_applicability(
|
||||||
Applicability::MaybeIncorrect,
|
diag,
|
||||||
|
"put the `;` here",
|
||||||
|
Applicability::MachineApplicable,
|
||||||
|
[(remove_span, String::new()), (insert_span, ";".to_owned())],
|
||||||
|
);
|
||||||
|
},
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
|
@ -10,7 +10,7 @@
|
||||||
|
|
||||||
macro_rules! m {
|
macro_rules! m {
|
||||||
(()) => {
|
(()) => {
|
||||||
()
|
();
|
||||||
};
|
};
|
||||||
(0) => {{
|
(0) => {{
|
||||||
0
|
0
|
||||||
|
@ -58,7 +58,7 @@ fn main() {
|
||||||
unit_fn_block();
|
unit_fn_block();
|
||||||
};
|
};
|
||||||
|
|
||||||
{ m!(()); }
|
{ m!(()) }
|
||||||
{ m!(()); }
|
{ m!(()); }
|
||||||
{ m!(()); };
|
{ m!(()); };
|
||||||
m!(0);
|
m!(0);
|
||||||
|
|
|
@ -2,15 +2,26 @@ error: consider moving the `;` inside the block for consistent formatting
|
||||||
--> $DIR/semicolon_inside_block.rs:39:5
|
--> $DIR/semicolon_inside_block.rs:39:5
|
||||||
|
|
|
|
||||||
LL | { unit_fn_block() };
|
LL | { unit_fn_block() };
|
||||||
| ^^^^^^^^^^^^^^^^^^^^ help: put the `;` here: `{ unit_fn_block(); }`
|
| ^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
|
|
||||||
= note: `-D clippy::semicolon-inside-block` implied by `-D warnings`
|
= note: `-D clippy::semicolon-inside-block` implied by `-D warnings`
|
||||||
|
help: put the `;` here
|
||||||
|
|
|
||||||
|
LL - { unit_fn_block() };
|
||||||
|
LL + { unit_fn_block(); }
|
||||||
|
|
|
||||||
|
|
||||||
error: consider moving the `;` inside the block for consistent formatting
|
error: consider moving the `;` inside the block for consistent formatting
|
||||||
--> $DIR/semicolon_inside_block.rs:40:5
|
--> $DIR/semicolon_inside_block.rs:40:5
|
||||||
|
|
|
|
||||||
LL | unsafe { unit_fn_block() };
|
LL | unsafe { unit_fn_block() };
|
||||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: put the `;` here: `unsafe { unit_fn_block(); }`
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
|
||||||
|
help: put the `;` here
|
||||||
|
|
|
||||||
|
LL - unsafe { unit_fn_block() };
|
||||||
|
LL + unsafe { unit_fn_block(); }
|
||||||
|
|
|
||||||
|
|
||||||
error: consider moving the `;` inside the block for consistent formatting
|
error: consider moving the `;` inside the block for consistent formatting
|
||||||
--> $DIR/semicolon_inside_block.rs:48:5
|
--> $DIR/semicolon_inside_block.rs:48:5
|
||||||
|
@ -23,17 +34,24 @@ LL | | };
|
||||||
|
|
|
|
||||||
help: put the `;` here
|
help: put the `;` here
|
||||||
|
|
|
|
||||||
LL ~ {
|
LL ~ unit_fn_block();
|
||||||
LL + unit_fn_block();
|
LL ~ }
|
||||||
LL + unit_fn_block();
|
|
||||||
LL + }
|
|
||||||
|
|
|
|
||||||
|
|
||||||
error: consider moving the `;` inside the block for consistent formatting
|
error: consider moving the `;` inside the block for consistent formatting
|
||||||
--> $DIR/semicolon_inside_block.rs:61:5
|
--> $DIR/semicolon_inside_block.rs:61:5
|
||||||
|
|
|
|
||||||
LL | { m!(()) };
|
LL | { m!(()) };
|
||||||
| ^^^^^^^^^^^ help: put the `;` here: `{ m!(()); }`
|
| ^^^^^^^^^^^
|
||||||
|
|
|
||||||
|
help: put the `;` here
|
||||||
|
|
|
||||||
|
LL ~ ();
|
||||||
|
LL | };
|
||||||
|
...
|
||||||
|
LL |
|
||||||
|
LL ~ { m!(()) }
|
||||||
|
|
|
||||||
|
|
||||||
error: aborting due to 4 previous errors
|
error: aborting due to 4 previous errors
|
||||||
|
|
||||||
|
|
|
@ -21,6 +21,9 @@ macro_rules! m {
|
||||||
(2) => {{
|
(2) => {{
|
||||||
2;
|
2;
|
||||||
}};
|
}};
|
||||||
|
(stmt) => {
|
||||||
|
stmt;
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
fn unit_fn_block() {
|
fn unit_fn_block() {
|
||||||
|
@ -39,8 +42,8 @@ fn main() {
|
||||||
{ unit_fn_block() };
|
{ unit_fn_block() };
|
||||||
unsafe { unit_fn_block() };
|
unsafe { unit_fn_block() };
|
||||||
|
|
||||||
{ unit_fn_block() };
|
{ unit_fn_block(); }
|
||||||
unsafe { unit_fn_block() };
|
unsafe { unit_fn_block(); }
|
||||||
|
|
||||||
{ unit_fn_block(); };
|
{ unit_fn_block(); };
|
||||||
unsafe { unit_fn_block(); };
|
unsafe { unit_fn_block(); };
|
||||||
|
@ -51,19 +54,20 @@ fn main() {
|
||||||
};
|
};
|
||||||
{
|
{
|
||||||
unit_fn_block();
|
unit_fn_block();
|
||||||
unit_fn_block()
|
unit_fn_block();
|
||||||
};
|
}
|
||||||
{
|
{
|
||||||
unit_fn_block();
|
unit_fn_block();
|
||||||
unit_fn_block();
|
unit_fn_block();
|
||||||
};
|
};
|
||||||
|
|
||||||
{ m!(()) };
|
{ m!(()) };
|
||||||
{ m!(()) };
|
{ m!(()); }
|
||||||
{ m!(()); };
|
{ m!(()); };
|
||||||
m!(0);
|
m!(0);
|
||||||
m!(1);
|
m!(1);
|
||||||
m!(2);
|
m!(2);
|
||||||
|
{ m!(stmt) };
|
||||||
|
|
||||||
for _ in [()] {
|
for _ in [()] {
|
||||||
unit_fn_block();
|
unit_fn_block();
|
||||||
|
|
|
@ -2,15 +2,26 @@ error: consider moving the `;` outside the block for consistent formatting
|
||||||
--> $DIR/semicolon_outside_block.rs:42:5
|
--> $DIR/semicolon_outside_block.rs:42:5
|
||||||
|
|
|
|
||||||
LL | { unit_fn_block(); }
|
LL | { unit_fn_block(); }
|
||||||
| ^^^^^^^^^^^^^^^^^^^^ help: put the `;` outside the block: `{ unit_fn_block() };`
|
| ^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
|
|
||||||
= note: `-D clippy::semicolon-outside-block` implied by `-D warnings`
|
= note: `-D clippy::semicolon-outside-block` implied by `-D warnings`
|
||||||
|
help: put the `;` here
|
||||||
|
|
|
||||||
|
LL - { unit_fn_block(); }
|
||||||
|
LL + { unit_fn_block() };
|
||||||
|
|
|
||||||
|
|
||||||
error: consider moving the `;` outside the block for consistent formatting
|
error: consider moving the `;` outside the block for consistent formatting
|
||||||
--> $DIR/semicolon_outside_block.rs:43:5
|
--> $DIR/semicolon_outside_block.rs:43:5
|
||||||
|
|
|
|
||||||
LL | unsafe { unit_fn_block(); }
|
LL | unsafe { unit_fn_block(); }
|
||||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: put the `;` outside the block: `unsafe { unit_fn_block() };`
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
|
||||||
|
help: put the `;` here
|
||||||
|
|
|
||||||
|
LL - unsafe { unit_fn_block(); }
|
||||||
|
LL + unsafe { unit_fn_block() };
|
||||||
|
|
|
||||||
|
|
||||||
error: consider moving the `;` outside the block for consistent formatting
|
error: consider moving the `;` outside the block for consistent formatting
|
||||||
--> $DIR/semicolon_outside_block.rs:52:5
|
--> $DIR/semicolon_outside_block.rs:52:5
|
||||||
|
@ -21,19 +32,23 @@ LL | | unit_fn_block();
|
||||||
LL | | }
|
LL | | }
|
||||||
| |_____^
|
| |_____^
|
||||||
|
|
|
|
||||||
help: put the `;` outside the block
|
help: put the `;` here
|
||||||
|
|
|
|
||||||
LL ~ {
|
LL ~ unit_fn_block()
|
||||||
LL + unit_fn_block();
|
LL ~ };
|
||||||
LL + unit_fn_block()
|
|
||||||
LL + };
|
|
||||||
|
|
|
|
||||||
|
|
||||||
error: consider moving the `;` outside the block for consistent formatting
|
error: consider moving the `;` outside the block for consistent formatting
|
||||||
--> $DIR/semicolon_outside_block.rs:62:5
|
--> $DIR/semicolon_outside_block.rs:62:5
|
||||||
|
|
|
|
||||||
LL | { m!(()); }
|
LL | { m!(()); }
|
||||||
| ^^^^^^^^^^^ help: put the `;` outside the block: `{ m!(()) };`
|
| ^^^^^^^^^^^
|
||||||
|
|
|
||||||
|
help: put the `;` here
|
||||||
|
|
|
||||||
|
LL - ()
|
||||||
|
LL + (); };
|
||||||
|
|
|
||||||
|
|
||||||
error: aborting due to 4 previous errors
|
error: aborting due to 4 previous errors
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue