-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Add compiler warning for double semicolon #60876
Comments
@rustbot modify labels: A-diagnostics C-enhancement E-easy |
Hi, I would like to resolve this issue. Do you have any lead/pointers that I can use to start? |
I'm not entirely sure, I only started recently with hacking the compiler. I think this can be easily done by looking at rust/src/libsyntax/parse/lexer/mod.rs Lines 947 to 950 in 49d139c
You could do something similar to this:
Of course you should also detect more than one, e.g. |
is it safe to just perform a while loop? Regardless how to say that this |
Go and try it.
That could be a problem indeed, because spaces are treated seperatly.
You mean something like
? I think this should be a different warning. Something like "sole `;` detected", because it does not end any statement at all and should have no right to exist. I think a person which is more privileged than me should say a word or too as well :) I'm not a rust lang member |
Instead of using the lexer level, you could also use the token level, because the lexer converts the bytestream to tokens (as you can see with |
This should be done as new a lint pass ( |
@Centril how does one go about doing that? |
A rustfix-able lint for this (along the line of |
We discussed this in the language team meeting today. People felt generally positive about such a lint. However, we would like to not lint on |
@hbina: I can give more detailed mentoring advice later, but for now: take a look at an existing lint (e.g. There's a slight problem at the moment, in that semicolons are stripped while parsing, and we'd need to preserve this information until we get to the point of linting. So you'll also need to modify the parser to not ignore semicolons (and store them in the statements). (Sorry, this is a bit rushed.) Feel free to message me on Discord or Zulip for more help, or ask on one of the compiler channels there. |
Is this issue currently being worked on? I'm interested in contributing. |
I've done some work on this, and have a working draft ready for review. The PR is at #62984, feedback is appreciated! |
@Centril Would you mind giving a short explanation? I am curious 😃 |
@BO41 What sort of explanation? We felt positive about this lint since it removes something which sometimes happens and which is a mistake, iirc. We don't want to lint on |
@Centril I think this issue is only talking about duplicate semicolons. fn fn_returning_void() {
other_fn_returning_void();
} |
@BO41 well, |
Thanks @Centril and @scottmcm for the explanation. This issue is about multiple semicolons. Not a single after
is not really what this is about. As |
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;; | ^ ```
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;; | ^ ```
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;; | ^ ```
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;; | ^ ```
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;; | ^ ```
This could should emit an error:
I think something similar to this would be a good one:
The text was updated successfully, but these errors were encountered: