-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Add semicolon-outside/inside-block lints #9826
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
93b5c89
Add semicolon-outside/inside-block lints
Veykril ba951e3
Fix formatting of let chains
Veykril 23744cd
Use multi-span suggestions
Veykril dd1163f
Fix macro statement handling
Veykril ab41546
Address reviews
Veykril f585414
Adjust semicolon block lint descriptions
Veykril f62eab4
Adjust description once more
Veykril 20ec2ce
Add test case for blocks with semicolon inside and outside a block
Veykril File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
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, LintContext}; | ||
use rustc_session::{declare_lint_pass, declare_tool_lint}; | ||
use rustc_span::Span; | ||
|
||
declare_clippy_lint! { | ||
/// ### What it does | ||
/// | ||
/// Suggests moving the semicolon from a block inside of the block to its kast expression. | ||
/// | ||
/// ### Why is this bad? | ||
/// | ||
/// For consistency it's best to have the semicolon inside/outside the block. Either way is fine and this lint suggests inside the block. | ||
/// Take a look at `semicolon_outside_block` for the other alternative. | ||
/// | ||
/// ### Example | ||
/// | ||
/// ```rust | ||
/// # fn f(_: u32) {} | ||
/// # let x = 0; | ||
/// unsafe { f(x) }; | ||
/// ``` | ||
/// Use instead: | ||
/// ```rust | ||
/// # fn f(_: u32) {} | ||
/// # let x = 0; | ||
/// unsafe { f(x); } | ||
/// ``` | ||
#[clippy::version = "1.66.0"] | ||
pub SEMICOLON_INSIDE_BLOCK, | ||
restriction, | ||
"add a semicolon inside the block" | ||
} | ||
declare_clippy_lint! { | ||
/// ### What it does | ||
/// | ||
/// Suggests moving the semicolon from a block's final expression outside of the block. | ||
/// | ||
/// ### Why is this bad? | ||
/// | ||
/// For consistency it's best to have the semicolon inside/outside the block. Either way is fine and this lint suggests outside the block. | ||
/// Take a look at `semicolon_inside_block` for the other alternative. | ||
/// | ||
/// ### Example | ||
/// | ||
/// ```rust | ||
/// # fn f(_: u32) {} | ||
/// # let x = 0; | ||
/// unsafe { f(x); } | ||
/// ``` | ||
/// Use instead: | ||
/// ```rust | ||
/// # fn f(_: u32) {} | ||
/// # let x = 0; | ||
/// unsafe { f(x) }; | ||
/// ``` | ||
#[clippy::version = "1.66.0"] | ||
pub SEMICOLON_OUTSIDE_BLOCK, | ||
restriction, | ||
"add a semicolon outside the block" | ||
} | ||
declare_lint_pass!(SemicolonBlock => [SEMICOLON_INSIDE_BLOCK, SEMICOLON_OUTSIDE_BLOCK]); | ||
|
||
impl LateLintPass<'_> for SemicolonBlock { | ||
fn check_stmt(&mut self, cx: &LateContext<'_>, stmt: &Stmt<'_>) { | ||
match stmt.kind { | ||
StmtKind::Expr(Expr { | ||
kind: ExprKind::Block(block, _), | ||
.. | ||
}) if !block.span.from_expansion() => { | ||
let Block { | ||
expr: None, | ||
stmts: [.., stmt], | ||
.. | ||
} = block else { return }; | ||
let &Stmt { | ||
kind: StmtKind::Semi(expr), | ||
span, | ||
.. | ||
} = stmt else { return }; | ||
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<'_>, tail: &Expr<'_>, semi_span: Span) { | ||
let insert_span = tail.span.source_callsite().shrink_to_hi(); | ||
let remove_span = semi_span.with_lo(block.span.hi()); | ||
|
||
span_lint_and_then( | ||
cx, | ||
SEMICOLON_INSIDE_BLOCK, | ||
semi_span, | ||
"consider moving the `;` inside the block for consistent formatting", | ||
|diag| { | ||
multispan_sugg_with_applicability( | ||
diag, | ||
"put the `;` here", | ||
Applicability::MachineApplicable, | ||
[(remove_span, String::new()), (insert_span, ";".to_owned())], | ||
); | ||
}, | ||
); | ||
} | ||
|
||
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( | ||
cx, | ||
SEMICOLON_OUTSIDE_BLOCK, | ||
block.span, | ||
"consider moving the `;` outside the block for consistent formatting", | ||
|diag| { | ||
multispan_sugg_with_applicability( | ||
diag, | ||
"put the `;` here", | ||
Applicability::MachineApplicable, | ||
[(remove_span, String::new()), (insert_span, ";".to_owned())], | ||
); | ||
}, | ||
); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// run-rustfix | ||
#![allow( | ||
unused, | ||
clippy::unused_unit, | ||
clippy::unnecessary_operation, | ||
clippy::no_effect, | ||
clippy::single_element_loop | ||
)] | ||
#![warn(clippy::semicolon_inside_block)] | ||
|
||
macro_rules! m { | ||
(()) => { | ||
() | ||
}; | ||
(0) => {{ | ||
0 | ||
};}; | ||
(1) => {{ | ||
1; | ||
}}; | ||
(2) => {{ | ||
2; | ||
}}; | ||
} | ||
|
||
fn unit_fn_block() { | ||
() | ||
} | ||
|
||
#[rustfmt::skip] | ||
fn main() { | ||
{ unit_fn_block() } | ||
unsafe { unit_fn_block() } | ||
|
||
{ | ||
unit_fn_block() | ||
} | ||
|
||
{ unit_fn_block(); } | ||
unsafe { unit_fn_block(); } | ||
|
||
{ unit_fn_block(); } | ||
unsafe { unit_fn_block(); } | ||
|
||
{ unit_fn_block(); }; | ||
unsafe { 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!(0); | ||
m!(1); | ||
m!(2); | ||
|
||
for _ in [()] { | ||
unit_fn_block(); | ||
} | ||
for _ in [()] { | ||
unit_fn_block() | ||
} | ||
|
||
let _d = || { | ||
unit_fn_block(); | ||
}; | ||
let _d = || { | ||
unit_fn_block() | ||
}; | ||
|
||
unit_fn_block() | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// run-rustfix | ||
#![allow( | ||
unused, | ||
clippy::unused_unit, | ||
clippy::unnecessary_operation, | ||
clippy::no_effect, | ||
clippy::single_element_loop | ||
)] | ||
#![warn(clippy::semicolon_inside_block)] | ||
|
||
macro_rules! m { | ||
(()) => { | ||
() | ||
}; | ||
(0) => {{ | ||
0 | ||
};}; | ||
(1) => {{ | ||
1; | ||
}}; | ||
(2) => {{ | ||
2; | ||
}}; | ||
} | ||
|
||
fn unit_fn_block() { | ||
() | ||
} | ||
|
||
#[rustfmt::skip] | ||
fn main() { | ||
{ unit_fn_block() } | ||
unsafe { unit_fn_block() } | ||
|
||
{ | ||
unit_fn_block() | ||
} | ||
|
||
{ unit_fn_block() }; | ||
unsafe { unit_fn_block() }; | ||
|
||
{ unit_fn_block(); } | ||
unsafe { unit_fn_block(); } | ||
|
||
{ unit_fn_block(); }; | ||
unsafe { 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!(0); | ||
m!(1); | ||
m!(2); | ||
|
||
for _ in [()] { | ||
unit_fn_block(); | ||
} | ||
for _ in [()] { | ||
unit_fn_block() | ||
} | ||
|
||
let _d = || { | ||
unit_fn_block(); | ||
}; | ||
let _d = || { | ||
unit_fn_block() | ||
}; | ||
|
||
unit_fn_block() | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
error: consider moving the `;` inside the block for consistent formatting | ||
--> $DIR/semicolon_inside_block.rs:39:5 | ||
| | ||
LL | { unit_fn_block() }; | ||
| ^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= 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 | ||
--> $DIR/semicolon_inside_block.rs:40:5 | ||
| | ||
LL | 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 | ||
--> $DIR/semicolon_inside_block.rs:48:5 | ||
| | ||
LL | / { | ||
LL | | unit_fn_block(); | ||
LL | | unit_fn_block() | ||
LL | | }; | ||
| |______^ | ||
| | ||
help: put the `;` here | ||
| | ||
LL ~ unit_fn_block(); | ||
LL ~ } | ||
| | ||
|
||
error: consider moving the `;` inside the block for consistent formatting | ||
--> $DIR/semicolon_inside_block.rs:61:5 | ||
| | ||
LL | { m!(()) }; | ||
| ^^^^^^^^^^^ | ||
| | ||
help: put the `;` here | ||
| | ||
LL - { m!(()) }; | ||
LL + { m!(()); } | ||
| | ||
|
||
error: aborting due to 4 previous errors | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I was a bit hasty on that commit I think, fixed it