2020-01-09 07:52:01 +01:00
|
|
|
use crate::{EarlyContext, EarlyLintPass, LintContext};
|
2020-04-27 23:26:11 +05:30
|
|
|
use rustc_ast::{Block, StmtKind};
|
2020-01-09 11:18:47 +01:00
|
|
|
use rustc_errors::Applicability;
|
2020-02-27 04:10:42 +01:00
|
|
|
use rustc_span::Span;
|
2019-07-30 13:48:39 -04:00
|
|
|
|
|
|
|
declare_lint! {
|
2020-09-08 15:09:57 -07:00
|
|
|
/// The `redundant_semicolons` lint detects unnecessary trailing
|
|
|
|
/// semicolons.
|
|
|
|
///
|
|
|
|
/// ### Example
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// let _ = 123;;
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// {{produces}}
|
|
|
|
///
|
|
|
|
/// ### Explanation
|
|
|
|
///
|
|
|
|
/// Extra semicolons are not needed, and may be removed to avoid confusion
|
|
|
|
/// and visual clutter.
|
2020-02-27 04:10:42 +01:00
|
|
|
pub REDUNDANT_SEMICOLONS,
|
2019-07-30 13:48:39 -04:00
|
|
|
Warn,
|
|
|
|
"detects unnecessary trailing semicolons"
|
|
|
|
}
|
|
|
|
|
2020-02-27 04:10:42 +01:00
|
|
|
declare_lint_pass!(RedundantSemicolons => [REDUNDANT_SEMICOLONS]);
|
2019-07-30 13:48:39 -04:00
|
|
|
|
2020-02-27 04:10:42 +01:00
|
|
|
impl EarlyLintPass for RedundantSemicolons {
|
|
|
|
fn check_block(&mut self, cx: &EarlyContext<'_>, block: &Block) {
|
|
|
|
let mut seq = None;
|
|
|
|
for stmt in block.stmts.iter() {
|
|
|
|
match (&stmt.kind, &mut seq) {
|
|
|
|
(StmtKind::Empty, None) => seq = Some((stmt.span, false)),
|
|
|
|
(StmtKind::Empty, Some(seq)) => *seq = (seq.0.to(stmt.span), true),
|
2020-12-07 18:10:48 -05:00
|
|
|
(_, seq) => maybe_lint_redundant_semis(cx, seq),
|
2019-07-30 13:48:39 -04:00
|
|
|
}
|
|
|
|
}
|
2020-12-07 18:10:48 -05:00
|
|
|
maybe_lint_redundant_semis(cx, &mut seq);
|
2020-02-27 04:10:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-07 18:10:48 -05:00
|
|
|
fn maybe_lint_redundant_semis(cx: &EarlyContext<'_>, seq: &mut Option<(Span, bool)>) {
|
2020-02-27 04:10:42 +01:00
|
|
|
if let Some((span, multiple)) = seq.take() {
|
2020-10-25 17:14:19 -04:00
|
|
|
// FIXME: Find a better way of ignoring the trailing
|
|
|
|
// semicolon from macro expansion
|
|
|
|
if span == rustc_span::DUMMY_SP {
|
|
|
|
return;
|
|
|
|
}
|
2020-11-27 09:36:59 -05:00
|
|
|
|
2020-02-27 04:10:42 +01:00
|
|
|
cx.struct_span_lint(REDUNDANT_SEMICOLONS, span, |lint| {
|
|
|
|
let (msg, rem) = if multiple {
|
|
|
|
("unnecessary trailing semicolons", "remove these semicolons")
|
|
|
|
} else {
|
|
|
|
("unnecessary trailing semicolon", "remove this semicolon")
|
|
|
|
};
|
|
|
|
lint.build(msg)
|
|
|
|
.span_suggestion(span, rem, String::new(), Applicability::MaybeIncorrect)
|
|
|
|
.emit();
|
|
|
|
});
|
2019-07-30 13:48:39 -04:00
|
|
|
}
|
|
|
|
}
|