diff options
author | Robert Griesemer <gri@golang.org> | 2015-03-01 20:59:15 -0800 |
---|---|---|
committer | Robert Griesemer <gri@golang.org> | 2015-03-02 18:58:57 +0000 |
commit | 133ecf9210d4e3560916c6e5ece2b91a97399a9d (patch) | |
tree | 105bc12ef0ea885ed2c100d21e82dd0a98223e5c | |
parent | 705f1dfb24353e2ea08365b033140fe6bc10bc63 (diff) | |
download | tools-133ecf9210d4e3560916c6e5ece2b91a97399a9d.tar.gz |
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>
-rw-r--r-- | go/types/stmt.go | 9 | ||||
-rw-r--r-- | go/types/testdata/stmt0.src | 1 |
2 files changed, 9 insertions, 1 deletions
diff --git a/go/types/stmt.go b/go/types/stmt.go index fa60f64..3917336 100644 --- a/go/types/stmt.go +++ b/go/types/stmt.go @@ -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] diff --git a/go/types/testdata/stmt0.src b/go/types/testdata/stmt0.src index 4c84036..073e83b 100644 --- a/go/types/testdata/stmt0.src +++ b/go/types/testdata/stmt0.src @@ -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: |