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: vet should warn when time.Time type (or types embed it) is used as map keys. #22978

Closed
go101 opened this issue Dec 4, 2017 · 3 comments

Comments

Projects
None yet
5 participants
@go101
Copy link

commented Dec 4, 2017

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

go version go1.9.2 linux/amd64

Does this issue reproduce with the latest release?

yes

What did you do?

package main

import "time"

type T struct {
	time.Time
}

var a map[T]string

func main() {
}

$ go vet a.go
$ [nothing reported]

What did you expect to see?

$ go vet a.go
$ time.Time is not a good map key type.

Like time.Time, there may be some other types, which are not capable to be used as map key types.

What did you see instead?

nothing reported

@go101 go101 changed the title go vet: vet should warn on struct types which embed time.Time when the struct types are used as map keys. go vet: vet should warn when time.Time type (or types embed it) is used as map keys. Dec 4, 2017

@bradfitz

This comment has been minimized.

Copy link
Member

commented Dec 4, 2017

I don't think this meets the bar for a vet check.

It's perfectly valid to use a time.Time as a map key, if you know what you're doing. I think it would be too noisy for valid code.

But somebody could do some analysis on GitHub to see what percentage of time.Time-keyed maps are valid.

@bradfitz bradfitz changed the title go vet: vet should warn when time.Time type (or types embed it) is used as map keys. proposal: cmd/vet: vet should warn when time.Time type (or types embed it) is used as map keys. Dec 4, 2017

@gopherbot gopherbot added this to the Proposal milestone Dec 4, 2017

@gopherbot gopherbot added the Proposal label Dec 4, 2017

@richardartoul

This comment has been minimized.

Copy link

commented Dec 4, 2017

I wrote a linter that detects this type of issue if you're interested: https://github.com/m3db/build-tools/blob/master/linters/badtime/README.md

I'm trying to get it integrated with gometalinter right now.

@rsc

This comment has been minimized.

Copy link
Contributor

commented Dec 4, 2017

Now that vet is part of go test, the bar is even higher for new checks. Because it is valid sometimes to have a time.Time as a map key, rejecting it in vet is not OK.

You might talk to @dominikh about adding this to megacheck or one of the other linters.

@rsc rsc closed this Dec 4, 2017

@golang golang locked and limited conversation to collaborators Dec 4, 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.