From 698f8c2f01ea549d77d7dc3338a12e04c11057b9 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Wed, 17 Apr 2024 14:02:58 +0200 Subject: Adding upstream version 1.64.0+dfsg1. Signed-off-by: Daniel Baumann --- compiler/rustc_lint/src/redundant_semicolon.rs | 58 ++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 compiler/rustc_lint/src/redundant_semicolon.rs (limited to 'compiler/rustc_lint/src/redundant_semicolon.rs') diff --git a/compiler/rustc_lint/src/redundant_semicolon.rs b/compiler/rustc_lint/src/redundant_semicolon.rs new file mode 100644 index 000000000..26f413453 --- /dev/null +++ b/compiler/rustc_lint/src/redundant_semicolon.rs @@ -0,0 +1,58 @@ +use crate::{EarlyContext, EarlyLintPass, LintContext}; +use rustc_ast::{Block, StmtKind}; +use rustc_errors::{fluent, Applicability}; +use rustc_span::Span; + +declare_lint! { + /// The `redundant_semicolons` lint detects unnecessary trailing + /// semicolons. + /// + /// ### Example + /// + /// ```rust + /// let _ = 123;; + /// ``` + /// + /// {{produces}} + /// + /// ### Explanation + /// + /// Extra semicolons are not needed, and may be removed to avoid confusion + /// and visual clutter. + pub REDUNDANT_SEMICOLONS, + Warn, + "detects unnecessary trailing semicolons" +} + +declare_lint_pass!(RedundantSemicolons => [REDUNDANT_SEMICOLONS]); + +impl EarlyLintPass for RedundantSemicolons { + fn check_block(&mut self, cx: &EarlyContext<'_>, block: &Block) { + let mut seq = None; + for stmt in block.stmts.iter() { + match (&stmt.kind, &mut seq) { + (StmtKind::Empty, None) => seq = Some((stmt.span, false)), + (StmtKind::Empty, Some(seq)) => *seq = (seq.0.to(stmt.span), true), + (_, seq) => maybe_lint_redundant_semis(cx, seq), + } + } + maybe_lint_redundant_semis(cx, &mut seq); + } +} + +fn maybe_lint_redundant_semis(cx: &EarlyContext<'_>, seq: &mut Option<(Span, bool)>) { + if let Some((span, multiple)) = seq.take() { + // FIXME: Find a better way of ignoring the trailing + // semicolon from macro expansion + if span == rustc_span::DUMMY_SP { + return; + } + + cx.struct_span_lint(REDUNDANT_SEMICOLONS, span, |lint| { + lint.build(fluent::lint::redundant_semicolons) + .set_arg("multiple", multiple) + .span_suggestion(span, fluent::lint::suggestion, "", Applicability::MaybeIncorrect) + .emit(); + }); + } +} -- cgit v1.2.3