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:
parent
705f1dfb24
commit
133ecf9210
|
@ -456,7 +456,14 @@ func (check *Checker) stmt(ctxt stmtContext, s ast.Stmt) {
|
||||||
check.invalidAST(s.Pos(), "incorrect form of type switch guard")
|
check.invalidAST(s.Pos(), "incorrect form of type switch guard")
|
||||||
return
|
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]
|
rhs = guard.Rhs[0]
|
||||||
|
|
||||||
|
|
|
@ -540,6 +540,7 @@ func typeswitches() {
|
||||||
}
|
}
|
||||||
|
|
||||||
switch x /* ERROR "declared but not used" */ := x.(type) {}
|
switch x /* ERROR "declared but not used" */ := x.(type) {}
|
||||||
|
switch _ /* ERROR "no new variable on left side of :=" */ := x.(type) {}
|
||||||
|
|
||||||
switch x := x.(type) {
|
switch x := x.(type) {
|
||||||
case int:
|
case int:
|
||||||
|
|
Loading…
Reference in New Issue