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

Comments

Projects
None yet
4 participants
@adonovan
Copy link

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

This comment has been minimized.

Copy link
Contributor

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

This comment has been minimized.

Copy link
Contributor

commented Jul 30, 2013

Comment 2:

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

@rsc

This comment has been minimized.

Copy link
Contributor

commented Jul 30, 2013

Comment 3:

Labels changed: added feature.

@adg

This comment has been minimized.

Copy link
Contributor

commented Aug 19, 2013

Comment 4:

Owner changed to @adg.

Status changed to Started.

@adg

This comment has been minimized.

Copy link
Contributor

commented Aug 21, 2013

Comment 5:

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

Status changed to Fixed.

@adonovan adonovan added fixed labels Aug 21, 2013

@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

This issue was closed.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
You can’t perform that action at this time.