| 
 | 1 | +use crate::lint::{EarlyLintPass, LintPass, EarlyContext, LintArray, LintContext};  | 
 | 2 | +use syntax::ast::{Stmt, StmtKind, ExprKind};  | 
 | 3 | +use syntax::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.node {  | 
 | 16 | +            if let ExprKind::Tup(ref v) = &expr.node {  | 
 | 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(  | 
 | 30 | +                                REDUNDANT_SEMICOLON,  | 
 | 31 | +                                stmt.span,  | 
 | 32 | +                                &msg  | 
 | 33 | +                            );  | 
 | 34 | +                            let suggest_msg = if multiple {  | 
 | 35 | +                                "remove these semicolons"  | 
 | 36 | +                            } else {  | 
 | 37 | +                                "remove this semicolon"  | 
 | 38 | +                            };  | 
 | 39 | +                            err.span_suggestion(  | 
 | 40 | +                                stmt.span,  | 
 | 41 | +                                &suggest_msg,  | 
 | 42 | +                                String::new(),  | 
 | 43 | +                                Applicability::MaybeIncorrect  | 
 | 44 | +                            );  | 
 | 45 | +                            err.emit();  | 
 | 46 | +                        }  | 
 | 47 | +                    }  | 
 | 48 | +                }  | 
 | 49 | +            }  | 
 | 50 | +        }  | 
 | 51 | +    }  | 
 | 52 | +}  | 
0 commit comments