@@ -123,11 +123,11 @@ func checkSwitchStatements_(
123
123
return true
124
124
}
125
125
126
- defaultCaseExists := false
126
+ var defaultCase * ast. CaseClause
127
127
for _ , stmt := range sw .Body .List {
128
128
caseCl := stmt .(* ast.CaseClause )
129
129
if isDefaultCase (caseCl ) {
130
- defaultCaseExists = true
130
+ defaultCase = caseCl
131
131
continue // nothing more to do if it's the default case
132
132
}
133
133
for _ , e := range caseCl .List {
@@ -158,11 +158,11 @@ func checkSwitchStatements_(
158
158
}
159
159
}
160
160
161
- defaultSuffices := fDefaultSignifiesExhaustive && defaultCaseExists
161
+ defaultSuffices := fDefaultSignifiesExhaustive && defaultCase != nil
162
162
shouldReport := len (hitlist ) > 0 && ! defaultSuffices
163
163
164
164
if shouldReport {
165
- reportSwitch (pass , sw , samePkg , tagType , em , hitlist , defaultCaseExists , file )
165
+ reportSwitch (pass , sw , defaultCase , samePkg , tagType , em , hitlist , file )
166
166
}
167
167
return true
168
168
})
@@ -237,20 +237,18 @@ func determineMissingOutput(missingMembers map[string]struct{}, em *enumMembers)
237
237
func reportSwitch (
238
238
pass * analysis.Pass ,
239
239
sw * ast.SwitchStmt ,
240
+ defaultCase * ast.CaseClause ,
240
241
samePkg bool ,
241
242
enumType * types.Named ,
242
243
em * enumMembers ,
243
244
missingMembers map [string ]struct {},
244
- defaultCaseExists bool ,
245
245
f * ast.File ,
246
246
) {
247
247
missingOutput := determineMissingOutput (missingMembers , em )
248
248
249
249
var fixes []analysis.SuggestedFix
250
- if ! defaultCaseExists {
251
- if fix , ok := computeFix (pass , pass .Fset , f , sw , enumType , samePkg , missingMembers ); ok {
252
- fixes = append (fixes , fix )
253
- }
250
+ if fix , ok := computeFix (pass , pass .Fset , f , sw , defaultCase , enumType , samePkg , missingMembers ); ok {
251
+ fixes = append (fixes , fix )
254
252
}
255
253
256
254
pass .Report (analysis.Diagnostic {
@@ -261,7 +259,7 @@ func reportSwitch(
261
259
})
262
260
}
263
261
264
- func computeFix (pass * analysis.Pass , fset * token.FileSet , f * ast.File , sw * ast.SwitchStmt , enumType * types.Named , samePkg bool , missingMembers map [string ]struct {}) (analysis.SuggestedFix , bool ) {
262
+ func computeFix (pass * analysis.Pass , fset * token.FileSet , f * ast.File , sw * ast.SwitchStmt , defaultCase * ast. CaseClause , enumType * types.Named , samePkg bool , missingMembers map [string ]struct {}) (analysis.SuggestedFix , bool ) {
265
263
// Function and method calls may be mutative, so we don't want to reuse the
266
264
// call expression in the about-to-be-inserted case clause body. So we just
267
265
// don't suggest a fix in such situations.
@@ -275,7 +273,7 @@ func computeFix(pass *analysis.Pass, fset *token.FileSet, f *ast.File, sw *ast.S
275
273
return analysis.SuggestedFix {}, false
276
274
}
277
275
278
- textEdits := []analysis.TextEdit {missingCasesTextEdit (fset , f , samePkg , sw , enumType , missingMembers )}
276
+ textEdits := []analysis.TextEdit {missingCasesTextEdit (fset , f , samePkg , sw , defaultCase , enumType , missingMembers )}
279
277
280
278
// need to add "fmt" import if "fmt" import doesn't already exist
281
279
if ! hasImportWithPath (fset , f , `"fmt"` ) {
@@ -396,7 +394,7 @@ func fmtImportTextEdit(fset *token.FileSet, f *ast.File) analysis.TextEdit {
396
394
}
397
395
}
398
396
399
- func missingCasesTextEdit (fset * token.FileSet , f * ast.File , samePkg bool , sw * ast.SwitchStmt , enumType * types.Named , missingMembers map [string ]struct {}) analysis.TextEdit {
397
+ func missingCasesTextEdit (fset * token.FileSet , f * ast.File , samePkg bool , sw * ast.SwitchStmt , defaultCase * ast. CaseClause , enumType * types.Named , missingMembers map [string ]struct {}) analysis.TextEdit {
400
398
// ... Construct insertion text for case clause and its body ...
401
399
402
400
var tag bytes.Buffer
@@ -443,9 +441,14 @@ func missingCasesTextEdit(fset *token.FileSet, f *ast.File, samePkg bool, sw *as
443
441
444
442
// ... Create the text edit ...
445
443
444
+ pos := sw .Body .Rbrace - 1 // put it as last case
445
+ if defaultCase != nil {
446
+ pos = defaultCase .Case - 2 // put it before the default case (why -2?)
447
+ }
448
+
446
449
return analysis.TextEdit {
447
- Pos : sw . Body . Rbrace - 1 ,
448
- End : sw . Body . Rbrace - 1 ,
450
+ Pos : pos ,
451
+ End : pos ,
449
452
NewText : []byte (insert ),
450
453
}
451
454
}
0 commit comments