]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_lint/src/redundant_semicolon.rs
Merge commit '4f3ab69ea0a0908260944443c739426cc384ae1a' into clippyup
[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(
52             REDUNDANT_SEMICOLONS,
53             span,
54             fluent::lint_redundant_semicolons,
55             |lint| {
56                 lint.set_arg("multiple", multiple).span_suggestion(
57                     span,
58                     fluent::suggestion,
59                     "",
60                     Applicability::MaybeIncorrect,
61                 )
62             },
63         );
64     }
65 }