Skip to content

Commit

Permalink
chore: simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez committed Jan 15, 2025
1 parent 4bbc4fb commit d554169
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 28 deletions.
48 changes: 21 additions & 27 deletions exptostd.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,10 +227,6 @@ func (a *analyzer) run(pass *analysis.Pass) (any, error) {
}

// constraints
for _, diagnostic := range resultExpConstraints.Diagnostics {
pass.Report(diagnostic)
}

a.suggestReplaceImport(pass, imports, resultExpConstraints.shouldKeepImport, pkgExpConstraints, pkgComp)

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

obj := pass.TypesInfo.Uses[ident]
if obj == nil {
return analysis.Diagnostic{}, false
}

pkg, ok := obj.(*types.PkgName)
if !ok {
return analysis.Diagnostic{}, false
}

if pkg.Imported().Path() != importPath {
if !isPackageUsed(pass, ident, importPath) {
return analysis.Diagnostic{}, false
}

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

obj := pass.TypesInfo.Uses[ident]
if obj == nil {
return
}

pkg, ok := obj.(*types.PkgName)
if !ok {
return
}

if pkg.Imported().Path() != pkgExpConstraints {
if !isPackageUsed(pass, ident, pkgExpConstraints) {
return
}

Expand Down Expand Up @@ -331,7 +307,7 @@ func (a *analyzer) detectConstraintsUsage(pass *analysis.Pass, expr ast.Expr, re
}
}

result.Diagnostics = append(result.Diagnostics, diagnostic)
pass.Report(diagnostic)
}

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

func isPackageUsed(pass *analysis.Pass, ident *ast.Ident, importPath string) bool {
obj := pass.TypesInfo.Uses[ident]
if obj == nil {
return false
}

pkg, ok := obj.(*types.PkgName)
if !ok {
return false
}

if pkg.Imported().Path() != importPath {
return false
}

return true
}

func getGoVersion(pass *analysis.Pass) int {
// Prior to go1.22, versions.FileVersion returns only the toolchain version,
// which is of no use to us,
Expand Down
2 changes: 1 addition & 1 deletion testdata/src/expconstraints/constraints.go.golden
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ func _[T cmp.Ordered](_ []T) bool { // want `golang.org/x/exp/constraints\.Order

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

0 comments on commit d554169

Please sign in to comment.