1
0
mirror of https://github.com/golang/go synced 2024-11-05 11:46:12 -07:00

cmd/vet: fix panic in dead code checker on ill-formed switch statements.

A switch statement without a tag requires case values to be bools, but
the parser does not enforce that, so AST-walking code needs to take
care.

Change-Id: I7d9abbb0324314e02a37813c2d2f6adb0d6af5e7
Reviewed-on: https://go-review.googlesource.com/107375
Reviewed-by: Rob Pike <r@golang.org>
This commit is contained in:
David Symonds 2018-04-16 09:41:05 +10:00
parent 0b9c1ad20d
commit 29eca06ff0
2 changed files with 10 additions and 1 deletions

View File

@ -45,7 +45,7 @@ func (f *File) updateDead(node ast.Node) {
} }
for _, expr := range cc.List { for _, expr := range cc.List {
v := f.pkg.types[expr].Value v := f.pkg.types[expr].Value
if v == nil || constant.BoolVal(v) { if v == nil || v.Kind() != constant.Bool || constant.BoolVal(v) {
continue BodyLoopBool continue BodyLoopBool
} }
} }

View File

@ -2123,3 +2123,12 @@ var _ = func() {
// goto without label used to panic // goto without label used to panic
goto goto
} }
func _() int {
// Empty switch tag with non-bool case value used to panic.
switch {
case 1:
println()
}
println()
}