Skip to content

Commit

Permalink
chore: compat & refactor
Browse files Browse the repository at this point in the history
Signed-off-by: Timon Wong <[email protected]>
  • Loading branch information
timonwong committed Oct 30, 2024
1 parent 7048ce0 commit 590af02
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
go: ['1.23']
go: ['1.22', '1.23']
steps:
- uses: actions/checkout@v4
- name: Set up Go
Expand Down
7 changes: 1 addition & 6 deletions internal/checkers/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,7 @@ func ExecuteChecker(c Checker, pass *analysis.Pass, call CallContext, cfg Config
startIndex := nparams - 1

lastArg := params.At(nparams - 1)
if iface, ok := lastArg.Type().(*types.Slice).Elem().(*types.Interface); !ok {
aface, ok := lastArg.Type().(*types.Slice).Elem().(*types.Alias) // slog uses any - an Alias not strictly an interface
if !ok || !aface.Underlying().(*types.Interface).Empty() {
return // final (args) param is not ...interface{}
}
} else if !iface.Empty() {
if !isTypeVariadicEmptyInterface(lastArg.Type()) {
return // final (args) param is not ...interface{}
}

Expand Down
14 changes: 3 additions & 11 deletions internal/checkers/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package checkers

import (
"go/ast"
"go/types"

"golang.org/x/tools/go/analysis"
)
Expand All @@ -15,19 +14,12 @@ func filterKeyAndValues(pass *analysis.Pass, keyAndValues []ast.Expr, objName st
switch arg := arg.(type) {
case *ast.CallExpr, *ast.Ident:
typ := pass.TypesInfo.TypeOf(arg)
switch typ := typ.(type) {
case *types.Alias, *types.Named:
var obj *types.TypeName
if cTyp, ok := typ.(*types.Alias); ok {
obj = cTyp.Obj()
} else {
obj = typ.(*types.Named).Obj()
}

if typ, ok := typ.(commonAlias); ok {
obj := typ.Obj()
if obj != nil && obj.Name() == objName {
continue
}
default:
// pass
}
}

Expand Down
27 changes: 27 additions & 0 deletions internal/checkers/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package checkers

import "go/types"

type commonAlias interface {
Obj() *types.TypeName
}

func isTypeVariadicEmptyInterface(typ types.Type) bool {
sliceTyp, ok := typ.(*types.Slice)
if !ok {
return false
}

Check warning on line 13 in internal/checkers/types.go

View check run for this annotation

Codecov / codecov/patch

internal/checkers/types.go#L12-L13

Added lines #L12 - L13 were not covered by tests

typ = sliceTyp.Elem()
for i := 0; i < 2; i++ {
switch iface := typ.(type) {
case *types.Interface:
return iface.Empty()
case *types.Alias:
typ = iface.Underlying()

Check warning on line 21 in internal/checkers/types.go

View check run for this annotation

Codecov / codecov/patch

internal/checkers/types.go#L20-L21

Added lines #L20 - L21 were not covered by tests
default:
return false
}
}
return false

Check warning on line 26 in internal/checkers/types.go

View check run for this annotation

Codecov / codecov/patch

internal/checkers/types.go#L26

Added line #L26 was not covered by tests
}

0 comments on commit 590af02

Please sign in to comment.