@@ -673,45 +673,35 @@ extern "C" {
673
673
"## ,
674
674
675
675
E0269 : r##"
676
- Functions must eventually return a value of their return type. For example, in
677
- the following function:
676
+ A returned value was expected but not all control paths return one.
677
+
678
+ Erroneous code example:
678
679
679
680
```compile_fail,E0269
680
681
fn abracada_FAIL() -> String {
681
682
"this won't work".to_string();
683
+ // error: not all control paths return a value
682
684
}
683
685
```
684
686
685
- If the condition is true, the value `x` is returned, but if the condition is
686
- false, control exits the `if` block and reaches a place where nothing is being
687
- returned. All possible control paths must eventually return a `u8`, which is not
688
- happening here.
689
-
690
- An easy fix for this in a complicated function is to specify a default return
691
- value, if possible:
687
+ In the previous code, the function is supposed to return a `String`, however,
688
+ the code returns nothing (because of the ';'). Another erroneous code would be:
692
689
693
- ```ignore
694
- fn foo(x: u8) -> u8 {
695
- if x > 0 {
696
- x // alternatively, `return x`
690
+ ```compile_fail
691
+ fn abracada_FAIL(b: bool) -> u32 {
692
+ if b {
693
+ 0
694
+ } else {
695
+ "a" // It fails because an `u32` was expected and something else is
696
+ // returned.
697
697
}
698
- // lots of other if branches
699
- 0 // return 0 if all else fails
700
698
}
701
699
```
702
700
703
701
It is advisable to find out what the unhandled cases are and check for them,
704
702
returning an appropriate value or panicking if necessary. Check if you need
705
- to remove a semicolon from the last expression, like in this case:
706
-
707
- ```ignore
708
- fn foo(x: u8) -> u8 {
709
- inner(2*x + 1);
710
- }
711
- ```
712
-
713
- The semicolon discards the return value of `inner`, instead of returning
714
- it from `foo`.
703
+ to remove a semicolon from the last expression, like in the first erroneous
704
+ code example.
715
705
"## ,
716
706
717
707
E0270 : r##"
0 commit comments