go: proposal: cmd/vet: warning on a special case of switch code block use.

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

go version go1.9.3 linux/amd64

Does this issue reproduce with the latest release?

Yes

What did you do?

package main

func alwaysFalse() bool {
	return false
}

func main() {
	switch alwaysFalse() {
	case true: println("1: true")
	case false: println("1: false")
	}
	
	switch alwaysFalse() // vet should warn here.
	{
	case true: println("2: true")
	case false: println("2: false")
	}
}

What did you expect to see?

go vet should warn on the second switch code block use.

What did you see instead?

go vet reports nothing.

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 16 (13 by maintainers)

Most upvoted comments

The second block is the same as

switch alwaysFalse(); {
case true: println("2: true")
case false: println("2: false")
}

To be honest, I was surprised to learn that that compiles. I doubt that it hits the “frequency” criterion for vet, but making gofmt put that on a single line would probably make the bug obvious enough.

maybe it shouldn’t compile, https://golang.org/ref/spec#ExprSwitchStmt specifies the “;”

It’s valid Go code, the semicolon is injected at EOL after ) being the last token on the line