The "missing return" error is defined in the spec, by requiring a function
to end in a terminating statement:
https://go.dev/ref/spec#Terminating_statements
The list is necessarily not complete. So it is necessarily more advisory
than anything else. What things to put in is mainly limited by how much
complexity it would be to specify it and how important we deem it.
Specifying this case seems pretty hard to specify (note that `i` could be
modified in the loop body, so this always terminating requires some pretty
complex statements about what is or is not in the loop body - in
particular, if you want to do it on a purely syntactical level).
It also also can be replaced by `func TestMethod() int { return 0 }`, which
is strictly better, more readable code, so I wouldn't even necessarily
agree that it's a false-positive error message: You *should* fix that.



On Mon, Jan 8, 2024 at 5:32 AM burak serdar <bser...@computer.org> wrote:

> This question came up on Stack Overflow today:
>
> The following code is giving a "missing return" error where it shouldn't:
>
> func TestMethod() int {
>    for i := 0; i < 10; i++ {
>        return 0
>    }
> }
>
> Looks like an overlooked case in control flow analysis.
>
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/golang-nuts/CAMV2RqosYR2bo-j5Xg77BCf-HKeBV3679zFNLrpoV5GwKntX%2BQ%40mail.gmail.com
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CAEkBMfHkLKgUKJh8P6ginmnMXqe_m2JjMD7X261ptZiZg1HkNA%40mail.gmail.com.

Reply via email to