]> git.lizzy.rs Git - rust.git/blob - src/librustc_lint/redundant_semicolon.rs
Rollup merge of #72153 - lcnr:exhaustively-match, r=pnkfelix
[rust.git] / src / librustc_lint / redundant_semicolon.rs
1 use crate::{EarlyContext, EarlyLintPass, LintContext};
2 use rustc_ast::ast::{Block, StmtKind};
3 use rustc_errors::Applicability;
4 use rustc_span::Span;
5
6 declare_lint! {
7     pub REDUNDANT_SEMICOLONS,
8     Warn,
9     "detects unnecessary trailing semicolons"
10 }
11
12 declare_lint_pass!(RedundantSemicolons => [REDUNDANT_SEMICOLONS]);
13
14 impl EarlyLintPass for RedundantSemicolons {
15     fn check_block(&mut self, cx: &EarlyContext<'_>, block: &Block) {
16         let mut seq = None;
17         for stmt in block.stmts.iter() {
18             match (&stmt.kind, &mut seq) {
19                 (StmtKind::Empty, None) => seq = Some((stmt.span, false)),
20                 (StmtKind::Empty, Some(seq)) => *seq = (seq.0.to(stmt.span), true),
21                 (_, seq) => maybe_lint_redundant_semis(cx, seq),
22             }
23         }
24         maybe_lint_redundant_semis(cx, &mut seq);
25     }
26 }
27
28 fn maybe_lint_redundant_semis(cx: &EarlyContext<'_>, seq: &mut Option<(Span, bool)>) {
29     if let Some((span, multiple)) = seq.take() {
30         cx.struct_span_lint(REDUNDANT_SEMICOLONS, span, |lint| {
31             let (msg, rem) = if multiple {
32                 ("unnecessary trailing semicolons", "remove these semicolons")
33             } else {
34                 ("unnecessary trailing semicolon", "remove this semicolon")
35             };
36             lint.build(msg)
37                 .span_suggestion(span, rem, String::new(), Applicability::MaybeIncorrect)
38                 .emit();
39         });
40     }
41 }