Text file src/cmd/gofmt/testdata/typeswitch.input

     1  /*
     2  Parenthesized type switch expressions originally
     3  accepted by gofmt must continue to be rewritten
     4  into the correct unparenthesized form.
     5  
     6  Only type-switches that didn't declare a variable
     7  in the type switch type assertion and which
     8  contained only "expression-like" (named) types in their
     9  cases were permitted to have their type assertion parenthesized
    10  by go/parser (due to a weak predicate in the parser). All others
    11  were rejected always, either with a syntax error in the
    12  type switch header or in the case.
    13  
    14  See also issue 4470.
    15  */
    16  package p
    17  
    18  func f() {
    19  	var x interface{}
    20  	switch x.(type) { // should remain the same
    21  	}
    22  	switch (x.(type)) { // should become: switch x.(type) {
    23  	}
    24  
    25  	switch x.(type) { // should remain the same
    26  	case int:
    27  	}
    28  	switch (x.(type)) { // should become: switch x.(type) {
    29  	case int:
    30  	}
    31  
    32  	switch x.(type) { // should remain the same
    33  	case []int:
    34  	}
    35  
    36  	// Parenthesized (x.(type)) in type switches containing cases
    37  	// with unnamed (literal) types were never permitted by gofmt;
    38  	// thus there won't be any code in the wild using this style if
    39  	// the code was gofmt-ed.
    40  	/*
    41  	switch (x.(type)) {
    42  	case []int:
    43  	}
    44  	*/
    45  
    46  	switch t := x.(type) { // should remain the same
    47  	default:
    48  		_ = t
    49  	}
    50  
    51  	// Parenthesized (x.(type)) in type switches declaring a variable
    52  	// were never permitted by gofmt; thus there won't be any code in
    53  	// the wild using this style if the code was gofmt-ed.
    54  	/*
    55  	switch t := (x.(type)) {
    56  	default:
    57  		_ = t
    58  	}
    59  	*/
    60  }
    61  

View as plain text