Skip to content

Commit 51b0107

Browse files
committed
New lint: unnecessary_semicolon
1 parent e692cd4 commit 51b0107

7 files changed

+148
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6173,6 +6173,7 @@ Released 2018-09-13
61736173
[`unnecessary_safety_comment`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_safety_comment
61746174
[`unnecessary_safety_doc`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_safety_doc
61756175
[`unnecessary_self_imports`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_self_imports
6176+
[`unnecessary_semicolon`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_semicolon
61766177
[`unnecessary_sort_by`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_sort_by
61776178
[`unnecessary_struct_initialization`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_struct_initialization
61786179
[`unnecessary_to_owned`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_to_owned

clippy_lints/src/declared_lints.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -756,6 +756,7 @@ pub static LINTS: &[&crate::LintInfo] = &[
756756
crate::unnecessary_map_on_constructor::UNNECESSARY_MAP_ON_CONSTRUCTOR_INFO,
757757
crate::unnecessary_owned_empty_strings::UNNECESSARY_OWNED_EMPTY_STRINGS_INFO,
758758
crate::unnecessary_self_imports::UNNECESSARY_SELF_IMPORTS_INFO,
759+
crate::unnecessary_semicolon::UNNECESSARY_SEMICOLON_INFO,
759760
crate::unnecessary_struct_initialization::UNNECESSARY_STRUCT_INITIALIZATION_INFO,
760761
crate::unnecessary_wraps::UNNECESSARY_WRAPS_INFO,
761762
crate::unneeded_struct_pattern::UNNEEDED_STRUCT_PATTERN_INFO,

clippy_lints/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,7 @@ mod unnecessary_literal_bound;
372372
mod unnecessary_map_on_constructor;
373373
mod unnecessary_owned_empty_strings;
374374
mod unnecessary_self_imports;
375+
mod unnecessary_semicolon;
375376
mod unnecessary_struct_initialization;
376377
mod unnecessary_wraps;
377378
mod unneeded_struct_pattern;
@@ -972,5 +973,6 @@ pub fn register_lints(store: &mut rustc_lint::LintStore, conf: &'static Conf) {
972973
store.register_late_pass(|_| Box::new(unnecessary_literal_bound::UnnecessaryLiteralBound));
973974
store.register_late_pass(move |_| Box::new(arbitrary_source_item_ordering::ArbitrarySourceItemOrdering::new(conf)));
974975
store.register_late_pass(|_| Box::new(unneeded_struct_pattern::UnneededStructPattern));
976+
store.register_late_pass(|_| Box::new(unnecessary_semicolon::UnnecessarySemicolon));
975977
// add lints here, do not remove this comment, it's used in `new_lint`
976978
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
use clippy_utils::diagnostics::span_lint_and_sugg;
2+
use rustc_errors::Applicability;
3+
use rustc_hir::{ExprKind, MatchSource, Stmt, StmtKind};
4+
use rustc_lint::{LateContext, LateLintPass};
5+
use rustc_session::declare_lint_pass;
6+
7+
declare_clippy_lint! {
8+
/// ### What it does
9+
/// Checks for the presence of a semicolon at the end of
10+
/// a `match` or `if` statement evaluating to `()`.
11+
///
12+
/// ### Why is this bad?
13+
/// The semicolon is not needed, and may be removed to
14+
/// avoid confusion and visual clutter.
15+
///
16+
/// ### Example
17+
/// ```no_run
18+
/// # let a: u32 = 42;
19+
/// if a > 10 {
20+
/// println!("a is greater than 10");
21+
/// };
22+
/// ```
23+
/// Use instead:
24+
/// ```no_run
25+
/// # let a: u32 = 42;
26+
/// if a > 10 {
27+
/// println!("a is greater than 10");
28+
/// }
29+
/// ```
30+
#[clippy::version = "1.86.0"]
31+
pub UNNECESSARY_SEMICOLON,
32+
pedantic,
33+
"unnecessary semicolon after expression returning `()`"
34+
}
35+
36+
declare_lint_pass!(UnnecessarySemicolon => [UNNECESSARY_SEMICOLON]);
37+
38+
impl LateLintPass<'_> for UnnecessarySemicolon {
39+
fn check_stmt(&mut self, cx: &LateContext<'_>, stmt: &Stmt<'_>) {
40+
// rustfmt already takes care of removing semicolons at the end
41+
// of loops.
42+
if let StmtKind::Semi(expr) = stmt.kind
43+
&& !stmt.span.from_expansion()
44+
&& !expr.span.from_expansion()
45+
&& matches!(
46+
expr.kind,
47+
ExprKind::If(..) | ExprKind::Match(_, _, MatchSource::Normal | MatchSource::Postfix)
48+
)
49+
&& cx.typeck_results().expr_ty(expr) == cx.tcx.types.unit
50+
{
51+
let semi_span = expr.span.shrink_to_hi().to(stmt.span.shrink_to_hi());
52+
span_lint_and_sugg(
53+
cx,
54+
UNNECESSARY_SEMICOLON,
55+
semi_span,
56+
"unnecessary semicolon",
57+
"remove",
58+
String::new(),
59+
Applicability::MachineApplicable,
60+
);
61+
}
62+
}
63+
}

tests/ui/unnecessary_semicolon.fixed

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#![warn(clippy::unnecessary_semicolon)]
2+
#![feature(postfix_match)]
3+
4+
fn no_lint(mut x: u32) -> Option<u32> {
5+
Some(())?;
6+
7+
{
8+
let y = 3;
9+
dbg!(x + y)
10+
};
11+
12+
{
13+
let (mut a, mut b) = (10, 20);
14+
(a, b) = (b + 1, a + 1);
15+
}
16+
17+
Some(0)
18+
}
19+
20+
fn main() {
21+
let mut a = 3;
22+
if a == 2 {
23+
println!("This is weird");
24+
}
25+
//~^ ERROR: unnecessary semicolon
26+
27+
a.match {
28+
3 => println!("three"),
29+
_ => println!("not three"),
30+
}
31+
//~^ ERROR: unnecessary semicolon
32+
}

tests/ui/unnecessary_semicolon.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#![warn(clippy::unnecessary_semicolon)]
2+
#![feature(postfix_match)]
3+
4+
fn no_lint(mut x: u32) -> Option<u32> {
5+
Some(())?;
6+
7+
{
8+
let y = 3;
9+
dbg!(x + y)
10+
};
11+
12+
{
13+
let (mut a, mut b) = (10, 20);
14+
(a, b) = (b + 1, a + 1);
15+
}
16+
17+
Some(0)
18+
}
19+
20+
fn main() {
21+
let mut a = 3;
22+
if a == 2 {
23+
println!("This is weird");
24+
};
25+
//~^ ERROR: unnecessary semicolon
26+
27+
a.match {
28+
3 => println!("three"),
29+
_ => println!("not three"),
30+
};
31+
//~^ ERROR: unnecessary semicolon
32+
}

tests/ui/unnecessary_semicolon.stderr

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
error: unnecessary semicolon
2+
--> tests/ui/unnecessary_semicolon.rs:24:6
3+
|
4+
LL | };
5+
| ^ help: remove
6+
|
7+
= note: `-D clippy::unnecessary-semicolon` implied by `-D warnings`
8+
= help: to override `-D warnings` add `#[allow(clippy::unnecessary_semicolon)]`
9+
10+
error: unnecessary semicolon
11+
--> tests/ui/unnecessary_semicolon.rs:30:6
12+
|
13+
LL | };
14+
| ^ help: remove
15+
16+
error: aborting due to 2 previous errors
17+

0 commit comments

Comments
 (0)