]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_lint/src/redundant_semicolon.rs
Rollup merge of #79227 - sasurau4:test/move-cell-test-to-lib-core, r=jyn514
[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::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 after_item_stmt = false;
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) => {
38                     maybe_lint_redundant_semis(cx, seq, after_item_stmt);
39                     after_item_stmt = matches!(stmt.kind, StmtKind::Item(_));
40                 }
41             }
42         }
43         maybe_lint_redundant_semis(cx, &mut seq, after_item_stmt);
44     }
45 }
46
47 fn maybe_lint_redundant_semis(
48     cx: &EarlyContext<'_>,
49     seq: &mut Option<(Span, bool)>,
50     after_item_stmt: bool,
51 ) {
52     if let Some((span, multiple)) = seq.take() {
53         // FIXME: Find a better way of ignoring the trailing
54         // semicolon from macro expansion
55         if span == rustc_span::DUMMY_SP {
56             return;
57         }
58
59         // FIXME: Lint on semicolons after item statements
60         // once doing so doesn't break bootstrapping
61         if after_item_stmt {
62             return;
63         }
64
65         cx.struct_span_lint(REDUNDANT_SEMICOLONS, span, |lint| {
66             let (msg, rem) = if multiple {
67                 ("unnecessary trailing semicolons", "remove these semicolons")
68             } else {
69                 ("unnecessary trailing semicolon", "remove this semicolon")
70             };
71             lint.build(msg)
72                 .span_suggestion(span, rem, String::new(), Applicability::MaybeIncorrect)
73                 .emit();
74         });
75     }
76 }