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

spec: Functions require unreachable return statement #20999

Closed
graymalkin opened this issue Jul 13, 2017 · 2 comments
Closed

spec: Functions require unreachable return statement #20999

graymalkin opened this issue Jul 13, 2017 · 2 comments

Comments

@graymalkin
Copy link

Go complains about missing return statement at the end of functions even when those return statements would be unreachable because all code paths appropriately return a value.

What version of Go are you using (go version)?

go version go1.6.3 linux/amd64

What operating system and processor architecture are you using (go env)?

GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/simon/golang"
GORACE=""
GOROOT="/usr/local/go"
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GO15VENDOREXPERIMENT="1"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0"
CXX="g++"
CGO_ENABLED="1"

What did you do?

This function emits an error message

func foo(x int) int {
     switch x {
     default:
     case 0:
          return 0
     case 1:
          return 1
     }
}
# github.com/graymalkin/unreachable
./unreachable.go:11: missing return at end of function

Despite a return at the end of the function being unreachable in the presence of a default case in the switch statement.

What did you expect to see?

A clean compilation

What did you see instead?

# github.com/graymalkin/unreachable
./unreachable.go:11: missing return at end of function
@graymalkin
Copy link
Author

graymalkin commented Jul 13, 2017

For clarity, the following function does not exhibit the error:

func foo(x int) int {
     switch x {
     default:
     case 0:
          return 0
     case 1:
          return 1
     }
     return 0
}

@graymalkin
Copy link
Author

Case statement apparently don't fall through by default, sorry for wasting time!

func foo(x int) int {
     switch x {
     default:
          fallthrough
     case 0:
          return 0
     case 1:
          return 1
     }
     return 0
}

@mikioh mikioh changed the title Functions require unreachable return statement spec: Functions require unreachable return statement Jul 21, 2017
@golang golang locked and limited conversation to collaborators Jul 21, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants