On Mon, 2024-01-08 at 06:21 +0100, 'Axel Wagner' via golang-nuts wrote:
> 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.
> > 

The case here is covered by point 5 of the terminating statements
definition. It is a for loop with a loop condition, so it is not a
terminating statement.

-- 
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/dc021e72298ce84c5a979b6d974cf72abd482c6c.camel%40kortschak.io.

Reply via email to