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

proposal: spec: disallow unused assignment to return variable #7485

Open
robpike opened this Issue Mar 6, 2014 · 3 comments

Comments

Projects
None yet
4 participants
@robpike
Copy link
Contributor

robpike commented Mar 6, 2014

From @taradinoc on twitter: 

// http://play.golang.org/p/TsxFgLP1l2

package main

import "fmt"

func main() {
    i := foo()
    fmt.Printf("Hello, %d\n", i)
}

func foo() (x int) {
    x = 7
    return 5
}


This compiles and runs fine, but if x were a local instead of a return variable, gc
would complain that it was unused. is this an inconsistency that should be fixed?
@rsc

This comment has been minimized.

Copy link
Contributor

rsc commented May 11, 2014

Comment 1:

The rationale behind excluding return values was that
func foo() (x int) {
    return 5
}
assigns to x (implicitly) and doesn't use the result. The spec says
"Implementation restriction: A compiler may make it illegal to declare a variable inside
a function body if the variable is never used."
I don't think x is inside the function body here. Both because of that and because gc
has accepted the program already, I don't think we can change this for Go 1.x.

Labels changed: added go2, removed release-go1.3maybe.

@griesemer

This comment has been minimized.

Copy link
Contributor

griesemer commented May 12, 2014

Comment 2:

Agreed. I think this is working correctly per spec (and implementation restriction, at
least for 1.x).
@rsc

This comment has been minimized.

Copy link
Contributor

rsc commented May 21, 2014

Comment 3:

Labels changed: added release-none.

@rsc rsc added this to the Unplanned milestone Apr 10, 2015

@rsc rsc removed release-none labels Apr 10, 2015

@rsc rsc changed the title spec: assignment to otherwise unused return variable not detected as error spec: disallow unused assignment to return variable Jun 17, 2017

@rsc rsc changed the title spec: disallow unused assignment to return variable proposal: spec: disallow unused assignment to return variable Jun 17, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment