-
Notifications
You must be signed in to change notification settings - Fork 12.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
encode
;
stmt w/o expr as StmtKind::Empty
- Loading branch information
Showing
19 changed files
with
73 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,41 @@ | ||
use crate::{EarlyContext, EarlyLintPass, LintContext}; | ||
use rustc_ast::ast::{ExprKind, Stmt, StmtKind}; | ||
use rustc_ast::ast::{Block, StmtKind}; | ||
use rustc_errors::Applicability; | ||
use rustc_span::Span; | ||
|
||
declare_lint! { | ||
pub REDUNDANT_SEMICOLON, | ||
pub REDUNDANT_SEMICOLONS, | ||
Warn, | ||
"detects unnecessary trailing semicolons" | ||
} | ||
|
||
declare_lint_pass!(RedundantSemicolon => [REDUNDANT_SEMICOLON]); | ||
declare_lint_pass!(RedundantSemicolons => [REDUNDANT_SEMICOLONS]); | ||
|
||
impl EarlyLintPass for RedundantSemicolon { | ||
fn check_stmt(&mut self, cx: &EarlyContext<'_>, stmt: &Stmt) { | ||
if let StmtKind::Semi(expr) = &stmt.kind { | ||
if let ExprKind::Tup(ref v) = &expr.kind { | ||
if v.is_empty() { | ||
// Strings of excess semicolons are encoded as empty tuple expressions | ||
// during the parsing stage, so we check for empty tuple expressions | ||
// which span only semicolons | ||
if let Ok(source_str) = cx.sess().source_map().span_to_snippet(stmt.span) { | ||
if source_str.chars().all(|c| c == ';') { | ||
let multiple = (stmt.span.hi() - stmt.span.lo()).0 > 1; | ||
let msg = if multiple { | ||
"unnecessary trailing semicolons" | ||
} else { | ||
"unnecessary trailing semicolon" | ||
}; | ||
cx.struct_span_lint(REDUNDANT_SEMICOLON, stmt.span, |lint| { | ||
let mut err = lint.build(&msg); | ||
let suggest_msg = if multiple { | ||
"remove these semicolons" | ||
} else { | ||
"remove this semicolon" | ||
}; | ||
err.span_suggestion( | ||
stmt.span, | ||
&suggest_msg, | ||
String::new(), | ||
Applicability::MaybeIncorrect, | ||
); | ||
err.emit(); | ||
}); | ||
} | ||
} | ||
} | ||
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), | ||
(_, seq) => maybe_lint_redundant_semis(cx, seq), | ||
} | ||
} | ||
maybe_lint_redundant_semis(cx, &mut seq); | ||
} | ||
} | ||
|
||
fn maybe_lint_redundant_semis(cx: &EarlyContext<'_>, seq: &mut Option<(Span, bool)>) { | ||
if let Some((span, multiple)) = seq.take() { | ||
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(); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/test/ui/lint/redundant-semicolon/redundant-semi-proc-macro.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
error: expected statement after outer attribute | ||
--> $DIR/attr-dangling-in-fn.rs:5:1 | ||
--> $DIR/attr-dangling-in-fn.rs:4:3 | ||
| | ||
LL | } | ||
| ^ | ||
LL | #[foo = "bar"] | ||
| ^^^^^^^^^^^^^^ | ||
|
||
error: aborting due to previous error | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters