1
0
mirror of https://github.com/golang/go synced 2024-11-18 16:14:46 -07:00

go/types: better error message for use of _ in type switch

Change-Id: If690d2d9607b3632451df2681c293835321ed9bd
Reviewed-on: https://go-review.googlesource.com/6413
Reviewed-by: Alan Donovan <adonovan@google.com>
This commit is contained in:
Robert Griesemer 2015-03-01 20:59:15 -08:00
parent 705f1dfb24
commit 133ecf9210
2 changed files with 9 additions and 1 deletions

View File

@ -456,7 +456,14 @@ func (check *Checker) stmt(ctxt stmtContext, s ast.Stmt) {
check.invalidAST(s.Pos(), "incorrect form of type switch guard")
return
}
check.recordDef(lhs, nil) // lhs variable is implicitly declared in each cause clause
if lhs.Name == "_" {
// _ := x.(type) is an invalid short variable declaration
check.softErrorf(lhs.Pos(), "no new variable on left side of :=")
lhs = nil // avoid declared but not used error below
} else {
check.recordDef(lhs, nil) // lhs variable is implicitly declared in each cause clause
}
rhs = guard.Rhs[0]

View File

@ -540,6 +540,7 @@ func typeswitches() {
}
switch x /* ERROR "declared but not used" */ := x.(type) {}
switch _ /* ERROR "no new variable on left side of :=" */ := x.(type) {}
switch x := x.(type) {
case int: