Skip to content

Getting 'missing return at end of function' after panic / in dead code #13094

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

Closed
dtylman opened this issue Oct 29, 2015 · 1 comment
Closed

Comments

@dtylman
Copy link

dtylman commented Oct 29, 2015

Can't compile a function with no return after panic.
IMHO, adding the 'panic' clause after 'throw' in the following example is a dead code and should be omitted.

package main
import "fmt"


func throw(message string){
    panic(message)
}

func foo(value int) int{
    switch value {
    case 1:
        return 2
    case 3:
        return 4
    }
    throw("Can't do this!")
    //panic("Can't do this!") // uncomment this to compile.
}

func main() {
    fmt.Printf("Hello world: %v", foo(1))

}

expected: code compiles.
got: ./test.go:18: missing return at end of function
version:
go version go1.5.1 darwin/amd64
go version go1.5.1 windows/amd64

@griesemer
Copy link
Contributor

Working as intended. The compiler doesn't look into function calls (throw) to determine if a function terminates or not. The spec rules are pretty clear about this.

@golang golang locked and limited conversation to collaborators Nov 4, 2016
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

3 participants