Rollup merge of #62984 - nathanwhit:extra_semi_lint, r=varkor

Add lint for excess trailing semicolons

Closes #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;;
   |                ^

```
This commit is contained in:
Mazdak Farrokhzad 2019-08-14 22:56:18 +02:00 committed by GitHub
commit 7178cf5f97
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 95 additions and 4 deletions

View file

@ -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

View file

@ -28,7 +28,10 @@ error: expected `{`, found `;`
LL | if not // lack of braces is [sic]
| -- this `if` statement has a condition, but no block
LL | println!("Then when?");
| ^ expected `{`
| ^
| |
| expected `{`
| help: try placing this code inside a block: `{ ; }`
error: unexpected `2` after identifier
--> $DIR/issue-46836-identifier-not-instead-of-negation.rs:26:24

View file

@ -3,4 +3,6 @@ fn main() {
//~^ ERROR found a documentation comment that doesn't document anything
//~| HELP maybe a comment was intended
;
//~^ WARNING unnecessary trailing semicolon
//~| HELP remove this semicolon
}

View file

@ -6,6 +6,14 @@ LL | /// hi
|
= help: doc comments must come before what they document, maybe a comment was intended with `//`?
warning: unnecessary trailing semicolon
--> $DIR/doc-before-semi.rs:5:5
|
LL | ;
| ^ help: remove this semicolon
|
= note: `#[warn(redundant_semicolon)]` on by default
error: aborting due to previous error
For more information about this error, try `rustc --explain E0585`.

View file

@ -9,7 +9,7 @@ extern crate test_macros;
#[recollect_attr]
fn a() {
let x: usize = "hello";;;;; //~ ERROR mismatched types
let x: usize = "hello"; //~ ERROR mismatched types
}
#[recollect_attr]

View file

@ -6,7 +6,7 @@ error[E0308]: mismatched types
error[E0308]: mismatched types
--> $DIR/span-preservation.rs:12:20
|
LL | let x: usize = "hello";;;;;
LL | let x: usize = "hello";
| ^^^^^^^ expected usize, found reference
|
= note: expected type `usize`