Skip to content

Commit d554169

Browse files
committed
chore: simplify
1 parent 4bbc4fb commit d554169

File tree

2 files changed

+22
-28
lines changed

2 files changed

+22
-28
lines changed

exptostd.go

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -227,10 +227,6 @@ func (a *analyzer) run(pass *analysis.Pass) (any, error) {
227227
}
228228

229229
// constraints
230-
for _, diagnostic := range resultExpConstraints.Diagnostics {
231-
pass.Report(diagnostic)
232-
}
233-
234230
a.suggestReplaceImport(pass, imports, resultExpConstraints.shouldKeepImport, pkgExpConstraints, pkgComp)
235231

236232
return nil, nil
@@ -250,17 +246,7 @@ func (a *analyzer) detectPackageUsage(pass *analysis.Pass,
250246
return analysis.Diagnostic{}, false
251247
}
252248

253-
obj := pass.TypesInfo.Uses[ident]
254-
if obj == nil {
255-
return analysis.Diagnostic{}, false
256-
}
257-
258-
pkg, ok := obj.(*types.PkgName)
259-
if !ok {
260-
return analysis.Diagnostic{}, false
261-
}
262-
263-
if pkg.Imported().Path() != importPath {
249+
if !isPackageUsed(pass, ident, importPath) {
264250
return analysis.Diagnostic{}, false
265251
}
266252

@@ -292,17 +278,7 @@ func (a *analyzer) detectConstraintsUsage(pass *analysis.Pass, expr ast.Expr, re
292278
return
293279
}
294280

295-
obj := pass.TypesInfo.Uses[ident]
296-
if obj == nil {
297-
return
298-
}
299-
300-
pkg, ok := obj.(*types.PkgName)
301-
if !ok {
302-
return
303-
}
304-
305-
if pkg.Imported().Path() != pkgExpConstraints {
281+
if !isPackageUsed(pass, ident, pkgExpConstraints) {
306282
return
307283
}
308284

@@ -331,7 +307,7 @@ func (a *analyzer) detectConstraintsUsage(pass *analysis.Pass, expr ast.Expr, re
331307
}
332308
}
333309

334-
result.Diagnostics = append(result.Diagnostics, diagnostic)
310+
pass.Report(diagnostic)
335311
}
336312

337313
func (a *analyzer) suggestReplaceImport(pass *analysis.Pass, imports map[string]*ast.ImportSpec, shouldKeep bool, importPath, stdPackage string) {
@@ -426,6 +402,24 @@ func suggestedFixForConstraintsOrder(selExpr *ast.SelectorExpr) (analysis.Sugges
426402
}, nil
427403
}
428404

405+
func isPackageUsed(pass *analysis.Pass, ident *ast.Ident, importPath string) bool {
406+
obj := pass.TypesInfo.Uses[ident]
407+
if obj == nil {
408+
return false
409+
}
410+
411+
pkg, ok := obj.(*types.PkgName)
412+
if !ok {
413+
return false
414+
}
415+
416+
if pkg.Imported().Path() != importPath {
417+
return false
418+
}
419+
420+
return true
421+
}
422+
429423
func getGoVersion(pass *analysis.Pass) int {
430424
// Prior to go1.22, versions.FileVersion returns only the toolchain version,
431425
// which is of no use to us,

testdata/src/expconstraints/constraints.go.golden

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,4 @@ func _[T cmp.Ordered](_ []T) bool { // want `golang.org/x/exp/constraints\.Order
2626

2727
func _[S []E, E cmp.Ordered](s S) E { // want `golang.org/x/exp/constraints\.Ordered can be replaced by cmp\.Ordered`
2828
return s[len(s)-1]
29-
}
29+
}

0 commit comments

Comments
 (0)