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