]> git.proxmox.com Git - rustc.git/blob - compiler/rustc_lint/src/redundant_semicolon.rs
New upstream version 1.63.0+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 // 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 let (msg, rem) = if multiple {
53 ("unnecessary trailing semicolons", "remove these semicolons")
54 } else {
55 ("unnecessary trailing semicolon", "remove this semicolon")
56 };
57 lint.build(msg).span_suggestion(span, rem, "", Applicability::MaybeIncorrect).emit();
58 });
59 }
60 }