]> git.proxmox.com Git - rustc.git/blame - compiler/rustc_lint/src/redundant_semicolon.rs
New upstream version 1.68.2+dfsg1
[rustc.git] / compiler / rustc_lint / src / redundant_semicolon.rs
CommitLineData
f25598a0 1use crate::{lints::RedundantSemicolonsDiag, EarlyContext, EarlyLintPass, LintContext};
3dfed10e 2use rustc_ast::{Block, StmtKind};
74b04a01 3use rustc_span::Span;
e1599b0c
XL
4
5declare_lint! {
1b1a35ee
XL
6 /// The `redundant_semicolons` lint detects unnecessary trailing
7 /// semicolons.
8 ///
9 /// ### Example
10 ///
11 /// ```rust
12 /// let _ = 123;;
13 /// ```
14 ///
15 /// {{produces}}
16 ///
17 /// ### Explanation
18 ///
19 /// Extra semicolons are not needed, and may be removed to avoid confusion
20 /// and visual clutter.
74b04a01 21 pub REDUNDANT_SEMICOLONS,
e1599b0c
XL
22 Warn,
23 "detects unnecessary trailing semicolons"
24}
25
74b04a01 26declare_lint_pass!(RedundantSemicolons => [REDUNDANT_SEMICOLONS]);
e1599b0c 27
74b04a01
XL
28impl EarlyLintPass for RedundantSemicolons {
29 fn check_block(&mut self, cx: &EarlyContext<'_>, block: &Block) {
30 let mut seq = None;
31 for stmt in block.stmts.iter() {
32 match (&stmt.kind, &mut seq) {
33 (StmtKind::Empty, None) => seq = Some((stmt.span, false)),
34 (StmtKind::Empty, Some(seq)) => *seq = (seq.0.to(stmt.span), true),
5869c6ff 35 (_, seq) => maybe_lint_redundant_semis(cx, seq),
e1599b0c
XL
36 }
37 }
5869c6ff 38 maybe_lint_redundant_semis(cx, &mut seq);
74b04a01
XL
39 }
40}
41
5869c6ff 42fn maybe_lint_redundant_semis(cx: &EarlyContext<'_>, seq: &mut Option<(Span, bool)>) {
74b04a01 43 if let Some((span, multiple)) = seq.take() {
29967ef6
XL
44 // FIXME: Find a better way of ignoring the trailing
45 // semicolon from macro expansion
46 if span == rustc_span::DUMMY_SP {
47 return;
48 }
fc512014 49
f25598a0 50 cx.emit_spanned_lint(
2b03887a
FG
51 REDUNDANT_SEMICOLONS,
52 span,
f25598a0 53 RedundantSemicolonsDiag { multiple, suggestion: span },
2b03887a 54 );
e1599b0c
XL
55 }
56}