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

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

Closed
robpike opened this issue Mar 6, 2014 · 5 comments
Closed

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

robpike opened this issue Mar 6, 2014 · 5 comments
Labels
FrozenDueToAge LanguageChange Proposal-FinalCommentPeriod v2 A language change or incompatible library change
Milestone

Comments

@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
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
Copy link
Contributor

Comment 2:

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

@rsc
Copy link
Contributor

rsc commented May 21, 2014

Comment 3:

Labels changed: added release-none.

@robpike robpike added accepted v2 A language change or incompatible library change labels May 21, 2014
@rsc rsc added this to the Unplanned milestone 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
@ianlancetaylor ianlancetaylor added the NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. label Dec 13, 2017
@gopherbot gopherbot removed the NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. label Aug 16, 2019
@gopherbot gopherbot added the NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. label Sep 3, 2019
@ianlancetaylor
Copy link
Contributor

Implementing this in the language could be done, but we would have to pay attention to any use of a return with no values. This would make the language rules more complicated for a very minor gain. Therefore, this is a likely decline. Leaving open for four weeks for final comments.

@ianlancetaylor ianlancetaylor added Proposal-FinalCommentPeriod and removed NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. labels Apr 21, 2020
@ianlancetaylor ianlancetaylor modified the milestones: Unplanned, Proposal Apr 21, 2020
@ianlancetaylor
Copy link
Contributor

No further comments.

@golang golang locked and limited conversation to collaborators May 19, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge LanguageChange Proposal-FinalCommentPeriod v2 A language change or incompatible library change
Projects
None yet
Development

No branches or pull requests

5 participants