]> git.proxmox.com Git - rustc.git/blob - compiler/rustc_lint/src/redundant_semicolon.rs
New upstream version 1.48.0~beta.8+dfsg1
[rustc.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 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 cx.struct_span_lint(REDUNDANT_SEMICOLONS, span, |lint| {
46 let (msg, rem) = if multiple {
47 ("unnecessary trailing semicolons", "remove these semicolons")
48 } else {
49 ("unnecessary trailing semicolon", "remove this semicolon")
50 };
51 lint.build(msg)
52 .span_suggestion(span, rem, String::new(), Applicability::MaybeIncorrect)
53 .emit();
54 });
55 }
56 }