New lint: unnecessary_semicolon

This commit is contained in:
Samuel Tardieu 2024-09-18 14:25:33 +02:00
parent e692cd4b30
commit 51b0107d28
7 changed files with 148 additions and 0 deletions

View file

@ -756,6 +756,7 @@ pub static LINTS: &[&crate::LintInfo] = &[
crate::unnecessary_map_on_constructor::UNNECESSARY_MAP_ON_CONSTRUCTOR_INFO,
crate::unnecessary_owned_empty_strings::UNNECESSARY_OWNED_EMPTY_STRINGS_INFO,
crate::unnecessary_self_imports::UNNECESSARY_SELF_IMPORTS_INFO,
crate::unnecessary_semicolon::UNNECESSARY_SEMICOLON_INFO,
crate::unnecessary_struct_initialization::UNNECESSARY_STRUCT_INITIALIZATION_INFO,
crate::unnecessary_wraps::UNNECESSARY_WRAPS_INFO,
crate::unneeded_struct_pattern::UNNEEDED_STRUCT_PATTERN_INFO,

View file

@ -372,6 +372,7 @@ mod unnecessary_literal_bound;
mod unnecessary_map_on_constructor;
mod unnecessary_owned_empty_strings;
mod unnecessary_self_imports;
mod unnecessary_semicolon;
mod unnecessary_struct_initialization;
mod unnecessary_wraps;
mod unneeded_struct_pattern;
@ -972,5 +973,6 @@ pub fn register_lints(store: &mut rustc_lint::LintStore, conf: &'static Conf) {
store.register_late_pass(|_| Box::new(unnecessary_literal_bound::UnnecessaryLiteralBound));
store.register_late_pass(move |_| Box::new(arbitrary_source_item_ordering::ArbitrarySourceItemOrdering::new(conf)));
store.register_late_pass(|_| Box::new(unneeded_struct_pattern::UnneededStructPattern));
store.register_late_pass(|_| Box::new(unnecessary_semicolon::UnnecessarySemicolon));
// add lints here, do not remove this comment, it's used in `new_lint`
}

View file

@ -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,
);
}
}
}