|
1 | 1 | use crate::{EarlyContext, EarlyLintPass, LintContext}; |
2 | | -use rustc_ast::ast::{ExprKind, Stmt, StmtKind}; |
| 2 | +use rustc_ast::ast::{Block, StmtKind}; |
3 | 3 | use rustc_errors::Applicability; |
| 4 | +use rustc_span::Span; |
4 | 5 |
|
5 | 6 | declare_lint! { |
6 | | - pub REDUNDANT_SEMICOLON, |
| 7 | + pub REDUNDANT_SEMICOLONS, |
7 | 8 | Warn, |
8 | 9 | "detects unnecessary trailing semicolons" |
9 | 10 | } |
10 | 11 |
|
11 | | -declare_lint_pass!(RedundantSemicolon => [REDUNDANT_SEMICOLON]); |
| 12 | +declare_lint_pass!(RedundantSemicolons => [REDUNDANT_SEMICOLONS]); |
12 | 13 |
|
13 | | -impl EarlyLintPass for RedundantSemicolon { |
14 | | - fn check_stmt(&mut self, cx: &EarlyContext<'_>, stmt: &Stmt) { |
15 | | - if let StmtKind::Semi(expr) = &stmt.kind { |
16 | | - if let ExprKind::Tup(ref v) = &expr.kind { |
17 | | - if v.is_empty() { |
18 | | - // Strings of excess semicolons are encoded as empty tuple expressions |
19 | | - // during the parsing stage, so we check for empty tuple expressions |
20 | | - // which span only semicolons |
21 | | - if let Ok(source_str) = cx.sess().source_map().span_to_snippet(stmt.span) { |
22 | | - if source_str.chars().all(|c| c == ';') { |
23 | | - let multiple = (stmt.span.hi() - stmt.span.lo()).0 > 1; |
24 | | - let msg = if multiple { |
25 | | - "unnecessary trailing semicolons" |
26 | | - } else { |
27 | | - "unnecessary trailing semicolon" |
28 | | - }; |
29 | | - cx.struct_span_lint(REDUNDANT_SEMICOLON, stmt.span, |lint| { |
30 | | - let mut err = lint.build(&msg); |
31 | | - let suggest_msg = if multiple { |
32 | | - "remove these semicolons" |
33 | | - } else { |
34 | | - "remove this semicolon" |
35 | | - }; |
36 | | - err.span_suggestion( |
37 | | - stmt.span, |
38 | | - &suggest_msg, |
39 | | - String::new(), |
40 | | - Applicability::MaybeIncorrect, |
41 | | - ); |
42 | | - err.emit(); |
43 | | - }); |
44 | | - } |
45 | | - } |
46 | | - } |
| 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), |
47 | 22 | } |
48 | 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 | + }); |
49 | 40 | } |
50 | 41 | } |
0 commit comments