]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_lint/src/redundant_semicolon.rs
Rollup merge of #107656 - jonhoo:bump-rust-installer, r=Mark-Simulacrum
[rust.git] / compiler / rustc_lint / src / redundant_semicolon.rs
1 use crate::{lints::RedundantSemicolonsDiag, EarlyContext, EarlyLintPass, LintContext};
2 use rustc_ast::{Block, StmtKind};
3 use rustc_span::Span;
4
5 declare_lint! {
6     /// The `redundant_semicolons` lint detects unnecessary trailing
7     /// semicolons.
8     ///
9     /// ### Example
10     ///
11     /// ```rust
12     /// let _ = 123;;
13     /// ```
14     ///
15     /// {{produces}}
16     ///
17     /// ### Explanation
18     ///
19     /// Extra semicolons are not needed, and may be removed to avoid confusion
20     /// and visual clutter.
21     pub REDUNDANT_SEMICOLONS,
22     Warn,
23     "detects unnecessary trailing semicolons"
24 }
25
26 declare_lint_pass!(RedundantSemicolons => [REDUNDANT_SEMICOLONS]);
27
28 impl EarlyLintPass for RedundantSemicolons {
29     fn check_block(&mut self, cx: &EarlyContext<'_>, block: &Block) {
30         let mut seq = None;
31         for stmt in block.stmts.iter() {
32             match (&stmt.kind, &mut seq) {
33                 (StmtKind::Empty, None) => seq = Some((stmt.span, false)),
34                 (StmtKind::Empty, Some(seq)) => *seq = (seq.0.to(stmt.span), true),
35                 (_, seq) => maybe_lint_redundant_semis(cx, seq),
36             }
37         }
38         maybe_lint_redundant_semis(cx, &mut seq);
39     }
40 }
41
42 fn maybe_lint_redundant_semis(cx: &EarlyContext<'_>, seq: &mut Option<(Span, bool)>) {
43     if let Some((span, multiple)) = seq.take() {
44         // FIXME: Find a better way of ignoring the trailing
45         // semicolon from macro expansion
46         if span == rustc_span::DUMMY_SP {
47             return;
48         }
49
50         cx.emit_spanned_lint(
51             REDUNDANT_SEMICOLONS,
52             span,
53             RedundantSemicolonsDiag { multiple, suggestion: span },
54         );
55     }
56 }