@@ -19,6 +19,18 @@ import (
19
19
"golang.org/x/tools/go/ast/inspector"
20
20
)
21
21
22
+ const (
23
+ pkgExpMaps = "golang.org/x/exp/maps"
24
+ pkgExpSlices = "golang.org/x/exp/slices"
25
+ pkgExpConstraints = "golang.org/x/exp/constraints"
26
+ )
27
+
28
+ const (
29
+ pkgMaps = "maps"
30
+ pkgSlices = "slices"
31
+ pkgComp = "cmp"
32
+ )
33
+
22
34
const (
23
35
go123 = 123
24
36
go121 = 121
@@ -153,16 +165,16 @@ func (a *analyzer) run(pass *analysis.Pass) (any, error) {
153
165
}
154
166
155
167
switch ident .Name {
156
- case "maps" :
157
- diagnostic , usage := a .detectPackageUsage (pass , a .mapsPkgReplacements , selExpr , ident , node , "golang.org/x/exp/maps" )
168
+ case pkgMaps :
169
+ diagnostic , usage := a .detectPackageUsage (pass , a .mapsPkgReplacements , selExpr , ident , node , pkgExpMaps )
158
170
if usage {
159
171
pass .Report (diagnostic )
160
172
}
161
173
162
174
shouldKeepExpMaps = shouldKeepExpMaps || ! usage
163
175
164
- case "slices" :
165
- diagnostic , usage := a .detectPackageUsage (pass , a .slicesPkgReplacements , selExpr , ident , node , "golang.org/x/exp/slices" )
176
+ case pkgSlices :
177
+ diagnostic , usage := a .detectPackageUsage (pass , a .slicesPkgReplacements , selExpr , ident , node , pkgExpSlices )
166
178
if usage {
167
179
resultExpSlices .Diagnostics = append (resultExpSlices .Diagnostics , diagnostic )
168
180
}
@@ -203,23 +215,23 @@ func (a *analyzer) run(pass *analysis.Pass) (any, error) {
203
215
})
204
216
205
217
// maps
206
- a .suggestReplaceImport (pass , imports , shouldKeepExpMaps , "golang.org/x/exp/maps" , "maps" )
218
+ a .suggestReplaceImport (pass , imports , shouldKeepExpMaps , pkgExpMaps , pkgMaps )
207
219
208
220
// slices
209
221
if resultExpSlices .shouldKeepImport {
210
222
for _ , diagnostic := range resultExpSlices .Diagnostics {
211
223
pass .Report (diagnostic )
212
224
}
213
225
} else {
214
- a .suggestReplaceImport (pass , imports , resultExpSlices .shouldKeepImport , "golang.org/x/exp/slices" , "slices" )
226
+ a .suggestReplaceImport (pass , imports , resultExpSlices .shouldKeepImport , pkgExpSlices , pkgSlices )
215
227
}
216
228
217
229
// constraints
218
230
for _ , diagnostic := range resultExpConstraints .Diagnostics {
219
231
pass .Report (diagnostic )
220
232
}
221
233
222
- a .suggestReplaceImport (pass , imports , resultExpConstraints .shouldKeepImport , "golang.org/x/exp/constraints" , "cmp" )
234
+ a .suggestReplaceImport (pass , imports , resultExpConstraints .shouldKeepImport , pkgExpConstraints , pkgComp )
223
235
224
236
return nil , nil
225
237
}
@@ -290,7 +302,7 @@ func (a *analyzer) detectConstraintsUsage(pass *analysis.Pass, expr ast.Expr, re
290
302
return
291
303
}
292
304
293
- if pkg .Imported ().Path () != "golang.org/x/exp/constraints" {
305
+ if pkg .Imported ().Path () != pkgExpConstraints {
294
306
return
295
307
}
296
308
@@ -307,7 +319,7 @@ func (a *analyzer) detectConstraintsUsage(pass *analysis.Pass, expr ast.Expr, re
307
319
308
320
diagnostic := analysis.Diagnostic {
309
321
Pos : selExpr .Pos (),
310
- Message : fmt .Sprintf ("golang.org/x/exp/constraints. %s can be replaced by %s" , selExpr .Sel .Name , rp .Text ),
322
+ Message : fmt .Sprintf ("%s. %s can be replaced by %s" , pkgExpConstraints , selExpr .Sel .Name , rp .Text ),
311
323
}
312
324
313
325
if rp .Suggested != nil {
@@ -370,7 +382,7 @@ func suggestedFixForClear(callExpr *ast.CallExpr) (analysis.SuggestedFix, error)
370
382
func suggestedFixForKeysOrValues (callExpr * ast.CallExpr ) (analysis.SuggestedFix , error ) {
371
383
s := & ast.CallExpr {
372
384
Fun : & ast.SelectorExpr {
373
- X : & ast.Ident {Name : "slices" },
385
+ X : & ast.Ident {Name : pkgSlices },
374
386
Sel : & ast.Ident {Name : "Collect" },
375
387
},
376
388
Args : []ast.Expr {callExpr },
@@ -394,7 +406,7 @@ func suggestedFixForKeysOrValues(callExpr *ast.CallExpr) (analysis.SuggestedFix,
394
406
395
407
func suggestedFixForConstraintsOrder (selExpr * ast.SelectorExpr ) (analysis.SuggestedFix , error ) {
396
408
s := & ast.SelectorExpr {
397
- X : & ast.Ident {Name : "cmp" },
409
+ X : & ast.Ident {Name : pkgComp },
398
410
Sel : & ast.Ident {Name : "Ordered" },
399
411
}
400
412
0 commit comments