-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e692cd4
commit 30209f1
Showing
7 changed files
with
148 additions
and
0 deletions.
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
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,63 @@ | ||
use clippy_utils::diagnostics::span_lint_and_sugg; | ||
use rustc_errors::Applicability; | ||
use rustc_hir::{ExprKind, MatchSource, Stmt, StmtKind}; | ||
use rustc_lint::{LateContext, LateLintPass}; | ||
use rustc_session::declare_lint_pass; | ||
|
||
declare_clippy_lint! { | ||
/// ### What it does | ||
/// Checks for the presence of a semicolon at the end of | ||
/// a `match` or `if` statement evaluating to `()`. | ||
/// | ||
/// ### Why is this bad? | ||
/// The semicolon is not needed, and may be removed to | ||
/// avoid confusion and visual clutter. | ||
/// | ||
/// ### Example | ||
/// ```no_run | ||
/// #let a: u32 = 42; | ||
/// if a > 10 { | ||
/// println!("a is greater than 10"); | ||
/// }; | ||
/// ``` | ||
/// Use instead: | ||
/// ```no_run | ||
/// #let a: u32 = 42; | ||
/// if a > 10 { | ||
/// println!("a is greater than 10"); | ||
/// } | ||
/// ``` | ||
#[clippy::version = "1.86.0"] | ||
pub UNNECESSARY_SEMICOLON, | ||
pedantic, | ||
"unnecessary semicolon after expression returning `()`" | ||
} | ||
|
||
declare_lint_pass!(UnnecessarySemicolon => [UNNECESSARY_SEMICOLON]); | ||
|
||
impl LateLintPass<'_> for UnnecessarySemicolon { | ||
fn check_stmt(&mut self, cx: &LateContext<'_>, stmt: &Stmt<'_>) { | ||
// rustfmt already takes care of removing semicolons at the end | ||
// of loops. | ||
if let StmtKind::Semi(expr) = stmt.kind | ||
&& !stmt.span.from_expansion() | ||
&& !expr.span.from_expansion() | ||
&& matches!( | ||
expr.kind, | ||
ExprKind::If(..) | ExprKind::Match(_, _, MatchSource::Normal | MatchSource::Postfix) | ||
) | ||
&& cx.typeck_results().expr_ty(expr) == cx.tcx.types.unit | ||
{ | ||
let semi_span = expr.span.shrink_to_hi().to(stmt.span.shrink_to_hi()); | ||
span_lint_and_sugg( | ||
cx, | ||
UNNECESSARY_SEMICOLON, | ||
semi_span, | ||
"unnecessary semicolon", | ||
"remove", | ||
String::new(), | ||
Applicability::MachineApplicable, | ||
); | ||
} | ||
} | ||
} |
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,32 @@ | ||
#![warn(clippy::unnecessary_semicolon)] | ||
#![feature(postfix_match)] | ||
|
||
fn no_lint(mut x: u32) -> Option<u32> { | ||
Some(())?; | ||
|
||
{ | ||
let y = 3; | ||
dbg!(x + y) | ||
}; | ||
|
||
{ | ||
let (mut a, mut b) = (10, 20); | ||
(a, b) = (b + 1, a + 1); | ||
} | ||
|
||
Some(0) | ||
} | ||
|
||
fn main() { | ||
let mut a = 3; | ||
if a == 2 { | ||
println!("This is weird"); | ||
} | ||
//~^ ERROR: unnecessary semicolon | ||
|
||
a.match { | ||
3 => println!("three"), | ||
_ => println!("not three"), | ||
} | ||
//~^ ERROR: unnecessary semicolon | ||
} |
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,32 @@ | ||
#![warn(clippy::unnecessary_semicolon)] | ||
#![feature(postfix_match)] | ||
|
||
fn no_lint(mut x: u32) -> Option<u32> { | ||
Some(())?; | ||
|
||
{ | ||
let y = 3; | ||
dbg!(x + y) | ||
}; | ||
|
||
{ | ||
let (mut a, mut b) = (10, 20); | ||
(a, b) = (b + 1, a + 1); | ||
} | ||
|
||
Some(0) | ||
} | ||
|
||
fn main() { | ||
let mut a = 3; | ||
if a == 2 { | ||
println!("This is weird"); | ||
}; | ||
//~^ ERROR: unnecessary semicolon | ||
|
||
a.match { | ||
3 => println!("three"), | ||
_ => println!("not three"), | ||
}; | ||
//~^ ERROR: unnecessary semicolon | ||
} |
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,17 @@ | ||
error: unnecessary semicolon | ||
--> tests/ui/unnecessary_semicolon.rs:24:6 | ||
| | ||
LL | }; | ||
| ^ help: remove | ||
| | ||
= note: `-D clippy::unnecessary-semicolon` implied by `-D warnings` | ||
= help: to override `-D warnings` add `#[allow(clippy::unnecessary_semicolon)]` | ||
|
||
error: unnecessary semicolon | ||
--> tests/ui/unnecessary_semicolon.rs:30:6 | ||
| | ||
LL | }; | ||
| ^ help: remove | ||
|
||
error: aborting due to 2 previous errors | ||
|