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