Skip to content
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

Fix E0269 error explanation #34471

Merged
merged 1 commit into from
Jun 28, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 15 additions & 25 deletions src/librustc/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -673,45 +673,35 @@ extern "C" {
"##,

E0269: r##"
Functions must eventually return a value of their return type. For example, in
the following function:
A returned value was expected but not all control paths return one.

Erroneous code example:

```compile_fail,E0269
fn abracada_FAIL() -> String {
"this won't work".to_string();
// error: not all control paths return a value
}
```

If the condition is true, the value `x` is returned, but if the condition is
false, control exits the `if` block and reaches a place where nothing is being
returned. All possible control paths must eventually return a `u8`, which is not
happening here.

An easy fix for this in a complicated function is to specify a default return
value, if possible:
In the previous code, the function is supposed to return a `String`, however,
the code returns nothing (because of the ';'). Another erroneous code would be:

```ignore
fn foo(x: u8) -> u8 {
if x > 0 {
x // alternatively, `return x`
```compile_fail
fn abracada_FAIL(b: bool) -> u32 {
if b {
0
} else {
"a" // It fails because an `u32` was expected and something else is
// returned.
}
// lots of other if branches
0 // return 0 if all else fails
}
```

It is advisable to find out what the unhandled cases are and check for them,
returning an appropriate value or panicking if necessary. Check if you need
to remove a semicolon from the last expression, like in this case:

```ignore
fn foo(x: u8) -> u8 {
inner(2*x + 1);
}
```

The semicolon discards the return value of `inner`, instead of returning
it from `foo`.
to remove a semicolon from the last expression, like in the first erroneous
code example.
"##,

E0270: r##"
Expand Down