1
0
mirror of https://github.com/golang/go synced 2024-11-20 05:54:43 -07:00

gofmt: test rewrite of (x.(type)) -> x.(type)

R=rsc
CC=golang-dev
https://golang.org/cl/6867062
This commit is contained in:
Robert Griesemer 2012-12-06 09:20:03 -08:00
parent 485673188d
commit 42a854b746
3 changed files with 122 additions and 1 deletions

View File

@ -84,6 +84,7 @@ var tests = []struct {
{"testdata/comments.input", ""}, {"testdata/comments.input", ""},
{"testdata/import.input", ""}, {"testdata/import.input", ""},
{"testdata/crlf.input", ""}, // test case for issue 3961; see also TestCRLF {"testdata/crlf.input", ""}, // test case for issue 3961; see also TestCRLF
{"testdata/typeswitch.input", ""}, // test case for issue 4470
} }
func TestRewrite(t *testing.T) { func TestRewrite(t *testing.T) {

View File

@ -0,0 +1,60 @@
/*
Parenthesized type switch expressions originally
accepted by gofmt must continue to be rewritten
into the correct unparenthesized form.
Only type-switches that didn't declare a variable
in the the type switch type assertion and which
contained only "expression-like" (named) types in their
cases were permitted to have their type assertion parenthesized
by go/parser (due to a weak predicate in the parser). All others
were rejected always, either with a syntax error in the
type switch header or in the case.
See also issue 4470.
*/
package p
func f() {
var x interface{}
switch x.(type) { // should remain the same
}
switch x.(type) { // should become: switch x.(type) {
}
switch x.(type) { // should remain the same
case int:
}
switch x.(type) { // should become: switch x.(type) {
case int:
}
switch x.(type) { // should remain the same
case []int:
}
// Parenthesized (x.(type)) in type switches containing cases
// with unnamed (literal) types were never permitted by gofmt;
// thus there won't be any code in the wild using this style if
// the code was gofmt-ed.
/*
switch (x.(type)) {
case []int:
}
*/
switch t := x.(type) { // should remain the same
default:
_ = t
}
// Parenthesized (x.(type)) in type switches declaring a variable
// were never permitted by gofmt; thus there won't be any code in
// the wild using this style if the code was gofmt-ed.
/*
switch t := (x.(type)) {
default:
_ = t
}
*/
}

60
src/cmd/gofmt/testdata/typeswitch.input vendored Normal file
View File

@ -0,0 +1,60 @@
/*
Parenthesized type switch expressions originally
accepted by gofmt must continue to be rewritten
into the correct unparenthesized form.
Only type-switches that didn't declare a variable
in the the type switch type assertion and which
contained only "expression-like" (named) types in their
cases were permitted to have their type assertion parenthesized
by go/parser (due to a weak predicate in the parser). All others
were rejected always, either with a syntax error in the
type switch header or in the case.
See also issue 4470.
*/
package p
func f() {
var x interface{}
switch x.(type) { // should remain the same
}
switch (x.(type)) { // should become: switch x.(type) {
}
switch x.(type) { // should remain the same
case int:
}
switch (x.(type)) { // should become: switch x.(type) {
case int:
}
switch x.(type) { // should remain the same
case []int:
}
// Parenthesized (x.(type)) in type switches containing cases
// with unnamed (literal) types were never permitted by gofmt;
// thus there won't be any code in the wild using this style if
// the code was gofmt-ed.
/*
switch (x.(type)) {
case []int:
}
*/
switch t := x.(type) { // should remain the same
default:
_ = t
}
// Parenthesized (x.(type)) in type switches declaring a variable
// were never permitted by gofmt; thus there won't be any code in
// the wild using this style if the code was gofmt-ed.
/*
switch t := (x.(type)) {
default:
_ = t
}
*/
}