@@ -188,10 +188,28 @@ func suggestedFixSwitch(stmt *ast.SwitchStmt, pkg *types.Package, info *types.In
188
188
}
189
189
190
190
func addDefaultCase (buf * bytes.Buffer , named * types.Named , expr ast.Expr ) {
191
+ var dottedBuf bytes.Buffer
192
+ // writeDotted emits a dotted path a.b.c.
193
+ var writeDotted func (e ast.Expr ) bool
194
+ writeDotted = func (e ast.Expr ) bool {
195
+ switch e := e .(type ) {
196
+ case * ast.SelectorExpr :
197
+ if ! writeDotted (e .X ) {
198
+ return false
199
+ }
200
+ dottedBuf .WriteByte ('.' )
201
+ dottedBuf .WriteString (e .Sel .Name )
202
+ return true
203
+ case * ast.Ident :
204
+ dottedBuf .WriteString (e .Name )
205
+ return true
206
+ }
207
+ return false
208
+ }
209
+
191
210
buf .WriteString ("\t default:\n " )
192
211
typeName := fmt .Sprintf ("%s.%s" , named .Obj ().Pkg ().Name (), named .Obj ().Name ())
193
- var dottedBuf bytes.Buffer
194
- if writeDotted (& dottedBuf , expr ) {
212
+ if writeDotted (expr ) {
195
213
// Switch tag expression is a dotted path.
196
214
// It is safe to re-evaluate it in the default case.
197
215
format := fmt .Sprintf ("unexpected %s: %%#v" , typeName )
@@ -202,23 +220,6 @@ func addDefaultCase(buf *bytes.Buffer, named *types.Named, expr ast.Expr) {
202
220
}
203
221
}
204
222
205
- // writeDotted emits a dotted path a.b.c.
206
- func writeDotted (buf * bytes.Buffer , e ast.Expr ) bool {
207
- switch e := e .(type ) {
208
- case * ast.SelectorExpr :
209
- if ! writeDotted (buf , e .X ) {
210
- return false
211
- }
212
- buf .WriteByte ('.' )
213
- buf .WriteString (e .Sel .Name )
214
- return true
215
- case * ast.Ident :
216
- buf .WriteString (e .Name )
217
- return true
218
- }
219
- return false
220
- }
221
-
222
223
func namedTypeFromTypeSwitch (stmt * ast.TypeSwitchStmt , info * types.Info ) * types.Named {
223
224
switch assign := stmt .Assign .(type ) {
224
225
case * ast.ExprStmt :
0 commit comments