File tree Expand file tree Collapse file tree 3 files changed +96
-46
lines changed Expand file tree Collapse file tree 3 files changed +96
-46
lines changed Original file line number Diff line number Diff line change @@ -950,14 +950,25 @@ func (p *printer) block(s *ast.BlockStmt, indent int) {
950
950
}
951
951
952
952
953
+ func isTypeName (x ast.Expr ) bool {
954
+ switch t := x .(type ) {
955
+ case * ast.Ident :
956
+ return true
957
+ case * ast.SelectorExpr :
958
+ return isTypeName (t .X )
959
+ }
960
+ return false
961
+ }
962
+
963
+
953
964
// TODO(gri): Decide if this should be used more broadly. The printing code
954
965
// knows when to insert parentheses for precedence reasons, but
955
966
// need to be careful to keep them around type expressions.
956
967
func stripParens (x ast.Expr , inControlClause bool ) ast.Expr {
957
968
for px , hasParens := x .(* ast.ParenExpr ); hasParens ; px , hasParens = x .(* ast.ParenExpr ) {
958
969
x = px .X
959
- if _ , isCompositeLit := x .(* ast.CompositeLit ); isCompositeLit && inControlClause {
960
- // composite literals inside control clauses need parens;
970
+ if cx , isCompositeLit := x .(* ast.CompositeLit ); inControlClause && isCompositeLit && isTypeName ( cx . Type ) {
971
+ // composite literals inside control clauses need parens if they start with a type name ;
961
972
// don't strip innermost layer
962
973
return px
963
974
}
Original file line number Diff line number Diff line change @@ -144,47 +144,71 @@ func _() {
144
144
for x := range []int{} {
145
145
use(x)
146
146
}
147
- for x := range ( []int{}) {
147
+ for x := range []int{} {
148
148
use(x)
149
149
} // no parens printed
150
150
}
151
151
152
152
153
153
// Don't remove mandatory parentheses around composite literals in control clauses.
154
154
func _() {
155
+ // strip no parentheses - no composite literals or composite literals don't start with a type name
155
156
if x {
156
- } // no ()'s
157
+ }
157
158
if x {
158
- } // no ()'s
159
- if ( []T{}) {
160
- } // ()
161
- if ( []T{}) {
162
- } // ()
163
- if ( []T{}) {
164
- } // ()
159
+ }
160
+ if []T{} {
161
+ }
162
+ if []T{} {
163
+ }
164
+ if []T{} {
165
+ }
165
166
166
167
for x {
167
- } // no ()'s
168
+ }
168
169
for x {
169
- } // no ()'s
170
- for ( []T{}) {
171
- } // ()
172
- for ( []T{}) {
173
- } // ()
174
- for ( []T{}) {
175
- } // ()
170
+ }
171
+ for []T{} {
172
+ }
173
+ for []T{} {
174
+ }
175
+ for []T{} {
176
+ }
176
177
177
178
switch x {
178
- } // no ()'s
179
+ }
179
180
switch x {
180
- } // no ()'s
181
- switch ([]T{}) {
182
- } // ()
183
- switch ([]T{}) {
184
- } // ()
185
-
186
- for _ = range ([]T{T{42}}) {
187
- } // ()
181
+ }
182
+ switch []T{} {
183
+ }
184
+ switch []T{} {
185
+ }
186
+
187
+ for _ = range []T{T{42}} {
188
+ }
189
+
190
+ // leave parentheses - composite literals start with a type name
191
+ if (T{}) {
192
+ }
193
+ if (T{}) {
194
+ }
195
+ if (T{}) {
196
+ }
197
+
198
+ for (T{}) {
199
+ }
200
+ for (T{}) {
201
+ }
202
+ for (T{}) {
203
+ }
204
+
205
+ switch (T{}) {
206
+ }
207
+ switch (T{}) {
208
+ }
209
+
210
+ for _ = range (T1{T{42}}) {
211
+ }
188
212
}
189
213
190
214
Original file line number Diff line number Diff line change @@ -113,24 +113,39 @@ func _() {
113
113
114
114
// Don't remove mandatory parentheses around composite literals in control clauses.
115
115
func _() {
116
- if (x) {} // no ()'s
117
- if (((x))) {} // no ()'s
118
- if ([]T{}) {} // ()
119
- if (([]T{})) {} // ()
120
- if ; (((([]T{})))) {} // ()
121
-
122
- for (x) {} // no ()'s
123
- for (((x))) {} // no ()'s
124
- for ([]T{}) {} // ()
125
- for (([]T{})) {} // ()
126
- for ; (((([]T{})))) ; {} // ()
127
-
128
- switch (x) {} // no ()'s
129
- switch (((x))) {} // no ()'s
130
- switch ([]T{}) {} // ()
131
- switch (([]T{})) {} // ()
132
-
133
- for _ = range ((([]T{T{42}}))) {} // ()
116
+ // strip parentheses - no composite literals or composite literals don't start with a type name
117
+ if (x) {}
118
+ if (((x))) {}
119
+ if ([]T{}) {}
120
+ if (([]T{})) {}
121
+ if ; (((([]T{})))) {}
122
+
123
+ for (x) {}
124
+ for (((x))) {}
125
+ for ([]T{}) {}
126
+ for (([]T{})) {}
127
+ for ; (((([]T{})))) ; {}
128
+
129
+ switch (x) {}
130
+ switch (((x))) {}
131
+ switch ([]T{}) {}
132
+ switch ; (((([]T{})))) {}
133
+
134
+ for _ = range ((([]T{T{42}}))) {}
135
+
136
+ // leave parentheses - composite literals start with a type name
137
+ if (T{}) {}
138
+ if ((T{})) {}
139
+ if ; ((((T{})))) {}
140
+
141
+ for (T{}) {}
142
+ for ((T{})) {}
143
+ for ; ((((T{})))) ; {}
144
+
145
+ switch (T{}) {}
146
+ switch ; ((((T{})))) {}
147
+
148
+ for _ = range (((T1{T{42}}))) {}
134
149
}
135
150
136
151
You can’t perform that action at this time.
0 commit comments