-
Notifications
You must be signed in to change notification settings - Fork 13.4k
encode ;
stmt without expr as StmtKind::Empty
#69506
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.