]> git.lizzy.rs Git - rust.git/blob - src/librustc_lint/redundant_semicolon.rs
Auto merge of #68943 - ecstatic-morse:no-useless-drop-on-enum-variants, r=matthewjasper
[rust.git] / src / librustc_lint / redundant_semicolon.rs
1 use crate::{EarlyContext, EarlyLintPass, LintContext};
2 use rustc_ast::ast::{ExprKind, Stmt, StmtKind};
3 use rustc_errors::Applicability;
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                             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                 }
47             }
48         }
49     }
50 }