forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of rust-lang#62984 - nathanwhit:extra_semi_lint, r=varkor
Add lint for excess trailing semicolons Closes rust-lang#60876. A caveat (not necessarily a negative, but something to consider) with this implementation is that excess semicolons after return/continue/break now also cause an 'unreachable statement' warning. For the following example: ``` fn main() { extra_semis(); } fn extra_semis() -> i32 { let mut sum = 0;;; for i in 0..10 { if i == 5 { continue;; } else if i == 9 { break;; } else { sum += i;; } } return sum;; } ``` The output is: ``` warning: unnecessary trailing semicolons --> src/main.rs:5:21 | 5 | let mut sum = 0;;; | ^^ help: remove these semicolons | = note: `#[warn(redundant_semicolon)]` on by default warning: unnecessary trailing semicolon --> src/main.rs:8:22 | 8 | continue;; | ^ help: remove this semicolon warning: unnecessary trailing semicolon --> src/main.rs:10:19 | 10 | break;; | ^ help: remove this semicolon warning: unnecessary trailing semicolon --> src/main.rs:12:22 | 12 | sum += i;; | ^ help: remove this semicolon warning: unnecessary trailing semicolon --> src/main.rs:15:16 | 15 | return sum;; | ^ help: remove this semicolon warning: unreachable statement --> src/main.rs:8:22 | 8 | continue;; | ^ | = note: `#[warn(unreachable_code)]` on by default warning: unreachable statement --> src/main.rs:10:19 | 10 | break;; | ^ warning: unreachable statement --> src/main.rs:15:16 | 15 | return sum;; | ^ ```
- Loading branch information
Showing
9 changed files
with
95 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
use crate::lint::{EarlyLintPass, LintPass, EarlyContext, LintArray, LintContext}; | ||
use syntax::ast::{Stmt, StmtKind, ExprKind}; | ||
use syntax::errors::Applicability; | ||
|
||
declare_lint! { | ||
pub REDUNDANT_SEMICOLON, | ||
Warn, | ||
"detects unnecessary trailing semicolons" | ||
} | ||
|
||
declare_lint_pass!(RedundantSemicolon => [REDUNDANT_SEMICOLON]); | ||
|
||
impl EarlyLintPass for RedundantSemicolon { | ||
fn check_stmt(&mut self, cx: &EarlyContext<'_>, stmt: &Stmt) { | ||
if let StmtKind::Semi(expr) = &stmt.node { | ||
if let ExprKind::Tup(ref v) = &expr.node { | ||
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" | ||
}; | ||
let mut err = cx.struct_span_lint( | ||
REDUNDANT_SEMICOLON, | ||
stmt.span, | ||
&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(); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
warning: unnecessary trailing semicolons | ||
--> $DIR/block-expr-precedence.rs:60:21 | ||
| | ||
LL | if (true) { 12; };;; -num; | ||
| ^^ help: remove these semicolons | ||
| | ||
= note: `#[warn(redundant_semicolon)]` on by default | ||
|
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