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

cmd/vet: report useless == nil comparisons #5437

Closed
adonovan opened this issue May 9, 2013 · 5 comments
Closed

cmd/vet: report useless == nil comparisons #5437

adonovan opened this issue May 9, 2013 · 5 comments
Milestone

Comments

@adonovan
Copy link
Member

adonovan commented May 9, 2013

This isn't a bug per se, but it's a bug pattern that tools could help with.

Given this code

type T struct {
     F *int
}

Let's say you change this exported field F into a private field f plus an exported
accessor, like so:

type T struct {
    f *int
}
func (t *T) F() *int { return t.f }

Now you have to rename all references to T.F into T.F().  Mostly you can rely on
compiler errors to help you find what you need to change... but not always.  Consider:

  if t.F == nil { ... }

Thanks to the new feature whereby t.f evaluates to a method closure, this statement is
still valid after the change, but is now a provably false condition, so the statement
has no effect.  This is not what the user intended.

It seems to me that a comparison of f==nil, where f is the name of a method or function,
or an expression that creates a bound function, is almost surely a mistake.

We could make it a compiler error, or we could make go vet report it.
@rsc
Copy link
Contributor

rsc commented Jun 3, 2013

Comment 1:

We can't make it a compiler error in Go 1.x. Perhaps in Go 2.
Go vet should definitely report it.

@rsc
Copy link
Contributor

rsc commented Jul 30, 2013

Comment 2:

Labels changed: added priority-later, go1.2, removed priority-triage.

@rsc
Copy link
Contributor

rsc commented Jul 30, 2013

Comment 3:

Labels changed: added feature.

@adg
Copy link
Contributor

adg commented Aug 19, 2013

Comment 4:

Owner changed to @adg.

Status changed to Started.

@adg
Copy link
Contributor

adg commented Aug 21, 2013

Comment 5:

This issue was closed by revision golang/tools@74ecc2c.

Status changed to Fixed.

@rsc rsc added this to the Go1.2 milestone Apr 14, 2015
@rsc rsc removed the go1.2 label Apr 14, 2015
@golang golang locked and limited conversation to collaborators Jun 24, 2016
@rsc rsc unassigned adg Jun 22, 2022
This issue was closed.
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

4 participants