I have noticed that when I write the following code, the compiler yields a missing return statement error:
// Similar loops make sense in retry patterns
// but this is just a simple example
func TestMethod() int {
for i := 0; i < 10; i++ {
return 0
}
}
this is while the following compiles without any error:
func TestMethod() int {
for {
return 0
}
}
The fist code is both logically and technically fine as there is no possible way the method cannot return. Is there any reason why the compiler is showing that error? or it is some sort of a missing logic or bug?
Expanding @Cerise Limón's comment into an answer, the assertion that "the first code is .. technically fine" is wrong.
The Go language specification says this:
And also this:
(emphasis added by me)
Examining the code in the first function we can see that these conditions of the specification are not met:
The function has a result parameter (the
intreturn value) so must end in a terminating statement, but the final statement of this function is aforstatement with a condition, which is not a "terminating statement", as defined by the specification.It may seem odd but is, in fact, technically correct.
Bonus Material
So why is the second function fine?
In this case, the final statement in this function is a
forwith no condition and with no break statement referring to the for loop, satisfying the language specification definition of a terminating statement.There is logic at work.
If a
forstatement with no condition contains abreak, then the loop may terminate so the function needs a return statement.If a
forstatement with no condition does not contain abreak(and has noreturnstatements), then the loop will not terminate (at least, not as the result of a normal execution path that requires a function return value).Also worth noting is that there is no control flow analysis to determine whether any
breakstatements are reachable; they only need to exist. e.g. the following will also trigger a "missing return" compilation error, even though thebreakis demonstrably unreachable: