-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Add semicolon inside/outside block lints #7564
Closed
+603
−0
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f1ce81f
Added semicolon_inside_block lint
1c3t3a 8a47d20
Added semicolon_outside_block lint
1c3t3a 595feab
Updated semicolon_outside_block lint to fix a bug
1c3t3a 2807523
Fixed error with `if` blocks
1c3t3a bbc3c21
Fixed error with `for` and `match` blocks
1c3t3a 27fe047
Added .fixed test file
1c3t3a ccbb200
Fixed .stderr files
1c3t3a ed4a438
Refactored lint to use `check_fn`
1c3t3a 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 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 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 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,75 @@ | ||
use crate::rustc_lint::LintContext; | ||
use clippy_utils::diagnostics::span_lint_and_sugg; | ||
use clippy_utils::in_macro; | ||
use clippy_utils::source::snippet_with_macro_callsite; | ||
use if_chain::if_chain; | ||
use rustc_errors::Applicability; | ||
use rustc_hir::{Block, ExprKind}; | ||
use rustc_lint::{LateContext, LateLintPass}; | ||
use rustc_session::{declare_lint_pass, declare_tool_lint}; | ||
|
||
declare_clippy_lint! { | ||
/// **What it does:** For () returning expressions, check that the semicolon is inside 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 inside the block. | ||
/// Take a look at `semicolon_outside_block` for the other alternative. | ||
/// | ||
/// **Known problems:** None. | ||
/// | ||
/// **Example:** | ||
/// | ||
/// ```rust | ||
/// unsafe { f(x) }; | ||
/// ``` | ||
/// Use instead: | ||
/// ```rust | ||
/// unsafe { f(x); } | ||
/// ``` | ||
pub SEMICOLON_INSIDE_BLOCK, | ||
pedantic, | ||
"add a semicolon inside the block" | ||
} | ||
|
||
declare_lint_pass!(SemicolonInsideBlock => [SEMICOLON_INSIDE_BLOCK]); | ||
|
||
impl LateLintPass<'_> for SemicolonInsideBlock { | ||
fn check_block(&mut self, cx: &LateContext<'tcx>, block: &'tcx Block<'tcx>) { | ||
if_chain! { | ||
if !in_macro(block.span); | ||
if let Some(expr) = block.expr; | ||
let t_expr = cx.typeck_results().expr_ty(expr); | ||
if t_expr.is_unit(); | ||
if let snippet = snippet_with_macro_callsite(cx, expr.span, "}"); | ||
if !snippet.ends_with("};") && !snippet.ends_with('}'); | ||
then { | ||
// filter out the desugared `for` loop | ||
if let ExprKind::DropTemps(..) = &expr.kind { | ||
return; | ||
} | ||
|
||
let expr_snip = snippet_with_macro_callsite(cx, expr.span, ".."); | ||
|
||
// check for the right suggestion and span, differs if the block spans | ||
// multiple lines | ||
let (suggestion, span) = if cx.sess().source_map().is_multiline(block.span) { | ||
(format!("{};", expr_snip), expr.span.source_callsite()) | ||
} else { | ||
let block_with_pot_sem = cx.sess().source_map().span_extend_to_next_char(block.span, '\n', false); | ||
let block_snip = snippet_with_macro_callsite(cx, block.span, ".."); | ||
|
||
(block_snip.replace(expr_snip.as_ref(), &format!("{};", expr_snip)), block_with_pot_sem) | ||
}; | ||
|
||
span_lint_and_sugg( | ||
cx, | ||
SEMICOLON_INSIDE_BLOCK, | ||
span, | ||
"consider moving the `;` inside the block for consistent formatting", | ||
"put the `;` here", | ||
suggestion, | ||
Applicability::MaybeIncorrect, | ||
); | ||
} | ||
} | ||
} | ||
} |
This file contains 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,160 @@ | ||
use crate::rustc_lint::LintContext; | ||
use clippy_utils::diagnostics::span_lint_and_sugg; | ||
use clippy_utils::in_macro; | ||
use clippy_utils::source::snippet_with_macro_callsite; | ||
use if_chain::if_chain; | ||
use rustc_errors::Applicability; | ||
use rustc_hir::intravisit::FnKind; | ||
use rustc_hir::ExprKind; | ||
use rustc_hir::{Block, Body, Expr, FnDecl, HirId, StmtKind}; | ||
use rustc_lint::{LateContext, LateLintPass}; | ||
use rustc_session::{declare_lint_pass, declare_tool_lint}; | ||
use rustc_span::BytePos; | ||
use rustc_span::Span; | ||
use std::ops::Add; | ||
|
||
declare_clippy_lint! { | ||
/// **What it does:** For () returning expressions, check that the semicolon is outside 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. | ||
/// | ||
/// **Known problems:** None. | ||
/// | ||
/// **Example:** | ||
/// | ||
/// ```rust | ||
/// unsafe { f(x); } | ||
/// ``` | ||
/// Use instead: | ||
/// ```rust | ||
/// unsafe { f(x) }; | ||
/// ``` | ||
pub SEMICOLON_OUTSIDE_BLOCK, | ||
pedantic, | ||
"add a semicolon outside the block" | ||
} | ||
|
||
declare_lint_pass!(SemicolonOutsideBlock => [SEMICOLON_OUTSIDE_BLOCK]); | ||
|
||
impl LateLintPass<'_> for SemicolonOutsideBlock { | ||
fn check_fn( | ||
&mut self, | ||
cx: &LateContext<'tcx>, | ||
fn_kind: FnKind<'tcx>, | ||
_: &'tcx FnDecl<'_>, | ||
body: &'tcx Body<'_>, | ||
_: Span, | ||
_: HirId, | ||
) { | ||
if let ExprKind::Block(block, ..) = body.value.kind { | ||
// also check this block if we're inside a closure | ||
if matches!(fn_kind, FnKind::Closure) { | ||
check_block(cx, block); | ||
} | ||
|
||
// iterate over the statements and check if we find a potential | ||
// block to check | ||
for stmt in block.stmts { | ||
match stmt.kind { | ||
StmtKind::Expr(Expr { | ||
kind: ExprKind::Block(bl, ..), | ||
.. | ||
}) | ||
| StmtKind::Semi(Expr { | ||
kind: ExprKind::Block(bl, ..), | ||
.. | ||
}) => check_block(cx, bl), | ||
_ => (), | ||
} | ||
} | ||
|
||
// check if the potential trailing expr is a block and check it if necessary | ||
if let Some(Expr { | ||
kind: ExprKind::Block(bl, ..), | ||
.. | ||
}) = block.expr | ||
{ | ||
check_block(cx, bl); | ||
} | ||
} | ||
} | ||
} | ||
|
||
/// Checks for a block if it's a target of this lint and spans a suggestion | ||
/// if applicable. This method also recurses through all other statements that | ||
/// are blocks. | ||
fn check_block(cx: &LateContext<'_>, block: &Block<'_>) { | ||
// check all subblocks | ||
for stmt in block.stmts { | ||
match stmt.kind { | ||
StmtKind::Expr(Expr { | ||
kind: ExprKind::Block(bl, ..), | ||
.. | ||
}) | ||
| StmtKind::Semi(Expr { | ||
kind: ExprKind::Block(bl, ..), | ||
.. | ||
}) => check_block(cx, bl), | ||
_ => (), | ||
} | ||
} | ||
// check the potential trailing expression | ||
if let Some(Expr { | ||
kind: ExprKind::Block(bl, ..), | ||
.. | ||
}) = block.expr | ||
{ | ||
check_block(cx, bl); | ||
} | ||
|
||
if_chain! { | ||
if !in_macro(block.span); | ||
if block.expr.is_none(); | ||
if let Some(last) = block.stmts.last(); | ||
if let StmtKind::Semi(expr) = last.kind; | ||
let t_expr = cx.typeck_results().expr_ty(expr); | ||
if t_expr.is_unit(); | ||
then { | ||
// make sure we're also having the semicolon at the end of the expression... | ||
let expr_w_sem = expand_span_to_semicolon(cx, expr.span); | ||
let expr_snip = snippet_with_macro_callsite(cx, expr_w_sem, ".."); | ||
let mut expr_sugg = expr_snip.to_string(); | ||
expr_sugg.pop(); | ||
|
||
// and the block | ||
let block_w_sem = expand_span_to_semicolon(cx, block.span); | ||
let mut block_snip: String = snippet_with_macro_callsite(cx, block_w_sem, "..").to_string(); | ||
if block_snip.ends_with('\n') { | ||
block_snip.pop(); | ||
} | ||
|
||
// retrieve the suggestion | ||
let suggestion = if block_snip.ends_with(';') { | ||
block_snip.replace(expr_snip.as_ref(), &format!("{}", expr_sugg.as_str())) | ||
} else { | ||
format!("{};", block_snip.replace(expr_snip.as_ref(), &format!("{}", expr_sugg.as_str()))) | ||
}; | ||
|
||
span_lint_and_sugg( | ||
cx, | ||
SEMICOLON_OUTSIDE_BLOCK, | ||
if block_snip.ends_with(';') { | ||
block_w_sem | ||
} else { | ||
block.span | ||
}, | ||
"consider moving the `;` outside the block for consistent formatting", | ||
"put the `;` outside the block", | ||
suggestion, | ||
Applicability::MaybeIncorrect, | ||
); | ||
} | ||
} | ||
} | ||
|
||
/// Takes a span and extends it until after a semicolon in the last line of the span. | ||
fn expand_span_to_semicolon<'tcx>(cx: &LateContext<'tcx>, expr_span: Span) -> Span { | ||
let expr_span_with_sem = cx.sess().source_map().span_extend_to_next_char(expr_span, ';', false); | ||
expr_span_with_sem.with_hi(expr_span_with_sem.hi().add(BytePos(1))) | ||
} |
This file contains 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,48 @@ | ||
#![warn(clippy::semicolon_inside_block)] | ||
|
||
unsafe fn f(arg: u32) {} | ||
|
||
fn main() { | ||
let x = 32; | ||
|
||
unsafe { f(x) }; | ||
} | ||
|
||
fn get_unit() {} | ||
|
||
fn fooooo() { | ||
unsafe { f(32) } | ||
} | ||
|
||
fn moin() { | ||
println!("Hello") | ||
} | ||
|
||
fn hello() { | ||
get_unit() | ||
} | ||
|
||
fn basic101(x: i32) { | ||
let y: i32; | ||
y = x + 1 | ||
} | ||
|
||
#[rustfmt::skip] | ||
fn closure_error() { | ||
let _d = || { | ||
hello() | ||
}; | ||
} | ||
|
||
fn my_own_block() { | ||
let x: i32; | ||
{ | ||
let y = 42; | ||
x = y + 1; | ||
basic101(x) | ||
} | ||
assert_eq!(43, 43) | ||
} | ||
|
||
#[rustfmt::skip] | ||
fn one_line_block() { println!("Foo") } |
This file contains 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,58 @@ | ||
error: consider moving the `;` inside the block for consistent formatting | ||
--> $DIR/semicolon_inside_block.rs:8:5 | ||
| | ||
LL | unsafe { f(x) }; | ||
| ^^^^^^^^^^^^^^^^ help: put the `;` here: `unsafe { f(x); }` | ||
| | ||
= note: `-D clippy::semicolon-inside-block` implied by `-D warnings` | ||
|
||
error: consider moving the `;` inside the block for consistent formatting | ||
--> $DIR/semicolon_inside_block.rs:14:5 | ||
| | ||
LL | unsafe { f(32) } | ||
| ^^^^^^^^^^^^^^^^ help: put the `;` here: `unsafe { f(32); }` | ||
|
||
error: consider moving the `;` inside the block for consistent formatting | ||
--> $DIR/semicolon_inside_block.rs:18:5 | ||
| | ||
LL | println!("Hello") | ||
| ^^^^^^^^^^^^^^^^^ help: put the `;` here: `println!("Hello");` | ||
|
||
error: consider moving the `;` inside the block for consistent formatting | ||
--> $DIR/semicolon_inside_block.rs:22:5 | ||
| | ||
LL | get_unit() | ||
| ^^^^^^^^^^ help: put the `;` here: `get_unit();` | ||
|
||
error: consider moving the `;` inside the block for consistent formatting | ||
--> $DIR/semicolon_inside_block.rs:27:5 | ||
| | ||
LL | y = x + 1 | ||
| ^^^^^^^^^ help: put the `;` here: `y = x + 1;` | ||
|
||
error: consider moving the `;` inside the block for consistent formatting | ||
--> $DIR/semicolon_inside_block.rs:33:9 | ||
| | ||
LL | hello() | ||
| ^^^^^^^ help: put the `;` here: `hello();` | ||
|
||
error: consider moving the `;` inside the block for consistent formatting | ||
--> $DIR/semicolon_inside_block.rs:44:5 | ||
| | ||
LL | assert_eq!(43, 43) | ||
| ^^^^^^^^^^^^^^^^^^ help: put the `;` here: `assert_eq!(43, 43);` | ||
|
||
error: consider moving the `;` inside the block for consistent formatting | ||
--> $DIR/semicolon_inside_block.rs:42:9 | ||
| | ||
LL | basic101(x) | ||
| ^^^^^^^^^^^ help: put the `;` here: `basic101(x);` | ||
|
||
error: consider moving the `;` inside the block for consistent formatting | ||
--> $DIR/semicolon_inside_block.rs:48:21 | ||
| | ||
LL | fn one_line_block() { println!("Foo") } | ||
| ^^^^^^^^^^^^^^^^^^^ help: put the `;` here: `{ println!("Foo"); }` | ||
|
||
error: aborting due to 9 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.
Why do we have to check a whole function here? Couldn't you just use
check_block
and then get the parent stmt and check if it is aStmtKind::Semi
?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.
Sure, but that way I would also suggest a semicolon after the block of an
fn
, or a match arm. I tried finding out if the block belongs to such things viaparent_iter
, but that didn't really work the way expected. I thoughtcheck_fn
makes a cleaner approach, as it only iterates over blocks that we're interested in.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.
The problem with this approach is, that you will only be able to allow this lint at the function scope and not directly on the block.