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 const in func body #17100

Closed
zimmski opened this issue Sep 14, 2016 · 8 comments
Closed

proposal: spec: disallow unused const in func body #17100

zimmski opened this issue Sep 14, 2016 · 8 comments
Labels
FrozenDueToAge Proposal v2 A language change or incompatible library change
Milestone

Comments

@zimmski
Copy link
Contributor

zimmski commented Sep 14, 2016

In retrospective to the discussion on golang-nuts https://groups.google.com/forum/#!topic/golang-nuts/F1KskeC2IdU I want to propose (I guess this should get the GO2 label) that it should be illegal to declare a constant inside a function body if the constant is never used.

E.g. this example https://play.golang.org/p/JG9dSa_VKg with an unused constant inside a function body should have a compile error "x declared and not used" like https://play.golang.org/p/KLzHVO5L7q has for an unused variable.

Such compile errors do already exist as is shown for variables, as well as for imports and labels. However, constants are at currently the exception. Even though there is an accepted explanation to why such usages are forbidden: "... Go refuses to compile programs with unused variables or imports, trading short-term convenience for long-term build speed and program clarity." [https://golang.org/doc/faq#unused_variables_and_imports]

@bradfitz bradfitz added the v2 A language change or incompatible library change label Sep 14, 2016
@bradfitz
Copy link
Contributor

I'll flag this for consideration for any hypothetical Go2. It's not a guarantee it will be accepted, though.

@quentinmit quentinmit added this to the Proposal milestone Sep 14, 2016
@minux
Copy link
Member

minux commented Sep 14, 2016 via email

@zimmski
Copy link
Contributor Author

zimmski commented Sep 14, 2016

https://golang.org/ref/spec#Variable_declarations states Implementation restriction: A compiler may make it illegal to declare a variable inside a function body if the variable is never used.. I would like to add the same restriction for constants.

@griesemer
Copy link
Contributor

I am against this, at least as long as there isn't a more compelling argument.

As @minux already pointed out, even for variables, it's an implementation restriction (though I admit that effectively that's the same as a spec rule). But as @ianlancetaylor has said in the golang-nuts discussion, the reason for the restriction is that a variable that has been assigned to but that's not used often indicates a problem in the code (and a variable that's declared but not used counts as 'assigned to' because it's initialized to its zero value). The same is true for labels. An unused constant really doesn't pose a problem.

Thus, the other rules are really motivated by problems encountered in praxis, which is not the case here. We'd rather have fewer rules, after all, not more.

@robpike
Copy link
Contributor

robpike commented Sep 15, 2016

The effect of an unused constant is equivalent to that of a comment, and may even serve as a comment. I too think this should not be changed.

@pwaller
Copy link
Contributor

pwaller commented Jan 6, 2017

One might want to define an "Enum-style" block of consts with some which happen to be unused now, but may be used later. Sure, you could comment out the consts you're not using right now, but that may be inconvenient if the block is large.

Your proposal is restricted to changing the behaviour to that consts defined in functions (and not at the package level). That would make consts in functions special compared with package consts, which would be surprising to anyone who didn't happen to know that fact. Imagine the poor user at the point of failure: "Why is the compiler complaining in this case? When it doesn't complain in that case?! This is a compiler bug!".

@ianlancetaylor
Copy link
Contributor

@pwaller In fairness, a similar distinction between function scope and package scope already exists. The compiler accepts unused variables at package scopes and rejects them in function scope (see the "implementation restriction" in https://golang.org/ref/spec#Variable_declarations).

@rsc rsc changed the title Spec: Make it illegal to declare a constant inside a function body if the constant is never used proposal: spec: disallow unused const in func body Jun 16, 2017
@ianlancetaylor
Copy link
Contributor

Thanks, but, based on the comments by @griesemer and @robpike, we are not going to do this.

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

No branches or pull requests

9 participants