-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Unclear error message with extra semicolon #30497
Comments
The crucial thing here is the semicolon after your statement. A block consists of a bunch of statements, terminated with semicolons, possibly followed by an expression, which is not terminated by a semicolon. Therefore, putting the semicolon after fn generate_secret_number() -> u32 {
rand::thread_rng().gen_range(1, 101)
} Note the lack of a semicolon. I hope that helps! This bug might still be useful as a documentation bug - the long explaination for |
There's usually a suggestion printed telling you to remove the trailing semicolon. It only works when the type of the last statement-expression is the same as the function return type, which isn't known in this case since the return type of cc #29396, which attempted to fix this |
I see, still getting the hang of when semicolons are required. But maybe a clearer error message is required. |
I updated the title to make it accurately reflect the issue. The compiler never considers the line to be unreachable either way, it just says that not all control paths return a value. Which is true, none of the 1 control paths return a value. |
In situations where the value of the last expression must be inferred, rustc will not emit the "you might need to remove the semicolon" warning, so at least note this in the extended description. Fixes: rust-lang#30497
…Gomez E0269: add suggestion to check for trailing semicolons In situations where the value of the last expression must be inferred, rustc will not emit the "you might need to remove the semicolon" warning, so at least note this in the extended description. Fixes: rust-lang#30497
…Gomez E0269: add suggestion to check for trailing semicolons In situations where the value of the last expression must be inferred, rustc will not emit the "you might need to remove the semicolon" warning, so at least note this in the extended description. Fixes: rust-lang#30497
If I take the following line from the Rust book and make a function out of it
Meaning something like
Then the compiler fails with
However if I add the return statement, which should be optional. It works
Shouldn't this be considered an expression by the compiler?
The text was updated successfully, but these errors were encountered: