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: cmd/vet: warn when zero values make explicit initialisation unnecessary #21362

Closed
jokeyrhyme opened this issue Aug 9, 2017 · 2 comments

Comments

Projects
None yet
3 participants
@jokeyrhyme
Copy link

commented Aug 9, 2017

Please answer these questions before submitting your issue. Thanks!

What version of Go are you using (go version)?

go version go1.8.3 darwin/amd64

What operating system and processor architecture are you using (go env)?

GOARCH="amd64"
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"

What did you do?

import (
  "sync"
  "time"
)

type Thing struct {
  mutex sync.RWMutex
  time time.Time
}

func NewThing() *Thing {
  return &Thing{
    mutex: sync.RWMutex{},
    time: time.Unix(0, 0),
  }
}

What did you expect to see?

Proposal: go vet should warn when initialising a value is unnecessary, i.e. when the zero value for the type is effectively equal to the explicitly initialised value

As someone new to the language, I have not memorised which types have a zero value and what those zero values are

I will look these up now that I know about them, but go vet could help me and others in this case

What did you see instead?

go vet does not indicate any issues about unnecessary initialisation

@gopherbot gopherbot added this to the Proposal milestone Aug 9, 2017

@gopherbot gopherbot added the Proposal label Aug 9, 2017

@ianlancetaylor

This comment has been minimized.

Copy link
Contributor

commented Aug 9, 2017

See cmd/vet/README for guidelines for vet checks. I think this one fails the "correctness" criterion: unnecessary initialization is not a bug.

@jokeyrhyme

This comment has been minimized.

Copy link
Author

commented Aug 9, 2017

@ianlancetaylor
yep, found it: https://github.com/golang/go/blob/master/src/cmd/vet/README

Correctness:

Vet's tools are about correctness, not style. A vet check must identify real or
potential bugs that could cause incorrect compilation or execution. A check that
only identifies stylistic points or alternative correct approaches to a situation
is not acceptable.

I agree that unnecessary initialisation is not a bug

I've filed this as a suggestion for golint instead: golang/lint#317

@jokeyrhyme jokeyrhyme closed this Aug 9, 2017

@golang golang locked and limited conversation to collaborators Aug 9, 2018

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.