]> git.lizzy.rs Git - rust.git/blob - src/librustc_lint/redundant_semicolon.rs
Rollup merge of #68341 - fusion-engineering-forks:instant-docs, r=Dylan-DPC
[rust.git] / src / librustc_lint / redundant_semicolon.rs
1 use crate::{EarlyContext, EarlyLintPass, LintContext};
2 use rustc_errors::Applicability;
3 use syntax::ast::{ExprKind, Stmt, StmtKind};
4
5 declare_lint! {
6     pub REDUNDANT_SEMICOLON,
7     Warn,
8     "detects unnecessary trailing semicolons"
9 }
10
11 declare_lint_pass!(RedundantSemicolon => [REDUNDANT_SEMICOLON]);
12
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                             let mut err = cx.struct_span_lint(REDUNDANT_SEMICOLON, stmt.span, &msg);
30                             let suggest_msg = if multiple {
31                                 "remove these semicolons"
32                             } else {
33                                 "remove this semicolon"
34                             };
35                             err.span_suggestion(
36                                 stmt.span,
37                                 &suggest_msg,
38                                 String::new(),
39                                 Applicability::MaybeIncorrect,
40                             );
41                             err.emit();
42                         }
43                     }
44                 }
45             }
46         }
47     }
48 }