-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Block expression should warning about missing return value #26577
Comments
Possibly related to #26576 |
It was found during the same thing. My understanding of what's happening is that the return type is being inferred so it assume that returning '()' is valid. Since I get the error when I actually specify a type for the block expression. |
Here is my suggestion: If the type of the block expression is inferred and there no return value, emit a warning. Suggested warning removal: specify an explicit return value as let the_other_sum = {
foo();
()
}; |
lints run after type-checking, so this won't help. |
Another, more heavy handed approach: During parsing, if a block expression occurs in an syntactic context that is expecting a value, then do the warning (at parse time) suggesting that one put in an explicit return value. So: fn quux() {
{
hi();
} // this is fine
let x = {
foo();
}; // this causes the warning
bar({
foo();
}); // this causes the warning too
} Update: I'm not 100% sure what I would then suggest for the block that forms a closure's body; we cannot tell before type-inference whether a closure is meant to return a value or not... |
Triage: the error message has improved
|
Yes this is so much better. |
…hton Add tests for several E-needstest issues This PR adds a number of tests for various `E-needstest` errors. These tend to have been left open for a long time and seem unlikely to be closed otherwise. Closes rust-lang#10876. Closes rust-lang#22892. Closes rust-lang#26448. Closes rust-lang#26577. Closes rust-lang#26619. Closes rust-lang#27054. Closes rust-lang#28587. Closes rust-lang#44127. Closes rust-lang#44255. Closes rust-lang#55731. Closes rust-lang#57781.
…hton Add tests for several E-needstest issues This PR adds a number of tests for various `E-needstest` errors. These tend to have been left open for a long time and seem unlikely to be closed otherwise. Closes rust-lang#10876. Closes rust-lang#22892. Closes rust-lang#26448. Closes rust-lang#26577. Closes rust-lang#26619. Closes rust-lang#27054. Closes rust-lang#28587. Closes rust-lang#44127. Closes rust-lang#44255. Closes rust-lang#55731. Closes rust-lang#57781.
…hton Add tests for several E-needstest issues This PR adds a number of tests for various `E-needstest` errors. These tend to have been left open for a long time and seem unlikely to be closed otherwise. Closes rust-lang#10876. Closes rust-lang#22892. Closes rust-lang#26448. Closes rust-lang#26577. Closes rust-lang#26619. Closes rust-lang#27054. Closes rust-lang#28587. Closes rust-lang#44127. Closes rust-lang#44255. Closes rust-lang#55731. Closes rust-lang#57781.
…hton Add tests for several E-needstest issues This PR adds a number of tests for various `E-needstest` errors. These tend to have been left open for a long time and seem unlikely to be closed otherwise. Closes rust-lang#10876. Closes rust-lang#22892. Closes rust-lang#26448. Closes rust-lang#26577. Closes rust-lang#26619. Closes rust-lang#27054. Closes rust-lang#28587. Closes rust-lang#44127. Closes rust-lang#44255. Closes rust-lang#55731. Closes rust-lang#57781.
Add tests for several E-needstest issues This PR adds a number of tests for various `E-needstest` errors. These tend to have been left open for a long time and seem unlikely to be closed otherwise. Closes #10876. Closes #26448. Closes #26577. Closes #26619. Closes #27054. Closes #44127. Closes #44255. Closes #55731. Closes #57781.
…hton Add tests for several E-needstest issues This PR adds a number of tests for various `E-needstest` errors. These tend to have been left open for a long time and seem unlikely to be closed otherwise. Closes rust-lang#10876. Closes rust-lang#26448. Closes rust-lang#26577. Closes rust-lang#26619. Closes rust-lang#27054. Closes rust-lang#44127. Closes rust-lang#44255. Closes rust-lang#55731. Closes rust-lang#57781.
Add tests for several E-needstest issues This PR adds a number of tests for various `E-needstest` errors. These tend to have been left open for a long time and seem unlikely to be closed otherwise. Closes #10876. Closes #26448. Closes #26577. Closes #26619. Closes #27054. Closes #44127. Closes #44255. Closes #55731. Closes #57781.
This is part of the Rust tutorial session. The whole purpose of the exercise was to find a mistake in the block expression.
See Exercise 2.1
http://pnkfelix.github.io/cyot/tutorial/exercises/ex_part_2
With rust compile this:
It outputs this error:
It should also put a warning for line 12: "possibly missing return value". This would catch the bug.
The text was updated successfully, but these errors were encountered: