diff --git a/.github/workflows/go-cross.yml b/.github/workflows/go-cross.yml index 25f8a28..38bbf8d 100644 --- a/.github/workflows/go-cross.yml +++ b/.github/workflows/go-cross.yml @@ -1,5 +1,12 @@ name: Go Matrix -on: [push, pull_request] + +on: + push: + branches: + - main + pull_request: + branches: + - main jobs: diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 2b167fb..3d1b264 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -5,6 +5,8 @@ on: branches: - main pull_request: + branches: + - main jobs: @@ -13,7 +15,7 @@ jobs: runs-on: ubuntu-latest env: GO_VERSION: stable - GOLANGCI_LINT_VERSION: v1.62.2 + GOLANGCI_LINT_VERSION: v2.1.1 CGO_ENABLED: 0 steps: @@ -30,7 +32,7 @@ jobs: # https://golangci-lint.run/usage/install#other-ci - name: Install golangci-lint ${{ env.GOLANGCI_LINT_VERSION }} - run: curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin ${GOLANGCI_LINT_VERSION} + run: curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/HEAD/install.sh | sh -s -- -b $(go env GOPATH)/bin ${GOLANGCI_LINT_VERSION} - name: Make run: make diff --git a/.golangci.yml b/.golangci.yml index e615d3e..e45135c 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -1,83 +1,78 @@ +version: "2" + +formatters: + enable: + - gci + - gofumpt + settings: + gofumpt: + extra-rules: true + linters: - enable-all: true + default: all disable: - - exportloopref # deprecated - - sqlclosecheck # not relevant (SQL) - - rowserrcheck # not relevant (SQL) - cyclop # duplicate of gocyclo - - lll - dupl - - nlreturn + - errchkjson - exhaustive - exhaustruct - - testpackage - - tparallel + - lll + - nilnil + - nlreturn + - nonamedreturns - paralleltest - prealloc + - rowserrcheck # not relevant (SQL) + - sqlclosecheck # not relevant (SQL) + - testpackage + - tparallel - varnamelen - - nilnil - - errchkjson - - nonamedreturns - -linters-settings: - govet: - enable-all: true - disable: - - fieldalignment - gocyclo: - min-complexity: 20 - goconst: - min-len: 5 - min-occurrences: 3 - misspell: - locale: US - funlen: - lines: -1 - statements: 40 - godox: - keywords: - - FIXME - gofumpt: - extra-rules: true - depguard: - rules: - main: - deny: - - pkg: "github.com/instana/testify" - desc: not allowed - - pkg: "github.com/pkg/errors" - desc: Should be replaced by standard lib errors package - wsl: - force-case-trailing-whitespace: 1 - allow-trailing-comment: true - gocritic: - enabled-tags: - - diagnostic - - style - - performance - disabled-checks: - - sloppyReassign - - rangeValCopy - - octalLiteral - - paramTypeCombine # already handle by gofumpt.extra-rules - settings: - hugeParam: - sizeThreshold: 100 - nolintlint: - require-specific: true - require-explanation: true + settings: + depguard: + rules: + main: + deny: + - pkg: github.com/instana/testify + desc: not allowed + - pkg: github.com/pkg/errors + desc: Should be replaced by standard lib errors package + funlen: + lines: -1 + statements: 40 + goconst: + min-len: 5 + min-occurrences: 3 + gocritic: + disabled-checks: + - sloppyReassign + - rangeValCopy + - octalLiteral + - paramTypeCombine # already handle by gofumpt.extra-rules + enabled-tags: + - diagnostic + - style + - performance + settings: + hugeParam: + sizeThreshold: 100 + gocyclo: + min-complexity: 20 + godox: + keywords: + - FIXME + govet: + disable: + - fieldalignment + enable-all: true + misspell: + locale: US + nolintlint: + require-explanation: true + require-specific: true + wsl: + force-case-trailing-whitespace: 1 + allow-trailing-comment: true issues: - exclude-use-default: false max-issues-per-linter: 0 max-same-issues: 0 - -output: - show-stats: true - sort-results: true - sort-order: - - linter - - file - -run: - timeout: 5m diff --git a/exptostd.go b/exptostd.go index cf6c5e8..036f923 100644 --- a/exptostd.go +++ b/exptostd.go @@ -55,7 +55,6 @@ type analyzer struct { constraintsPkgReplacements map[string]stdReplacement[*ast.SelectorExpr] skipGoVersionDetection bool - goVersion int } // NewAnalyzer create a new Analyzer. @@ -126,7 +125,7 @@ func (a *analyzer) run(pass *analysis.Pass) (any, error) { return nil, nil } - a.goVersion = getGoVersion(pass) + goVersion := getGoVersion(pass) nodeFilter := []ast.Node{ (*ast.CallExpr)(nil), @@ -166,7 +165,7 @@ func (a *analyzer) run(pass *analysis.Pass) (any, error) { switch ident.Name { case pkgMaps: - diagnostic, usage := a.detectPackageUsage(pass, a.mapsPkgReplacements, selExpr, ident, node, pkgExpMaps) + diagnostic, usage := a.detectPackageUsage(pass, a.mapsPkgReplacements, selExpr, ident, node, pkgExpMaps, goVersion) if usage { pass.Report(diagnostic) } @@ -174,7 +173,7 @@ func (a *analyzer) run(pass *analysis.Pass) (any, error) { shouldKeepExpMaps = shouldKeepExpMaps || !usage case pkgSlices: - diagnostic, usage := a.detectPackageUsage(pass, a.slicesPkgReplacements, selExpr, ident, node, pkgExpSlices) + diagnostic, usage := a.detectPackageUsage(pass, a.slicesPkgReplacements, selExpr, ident, node, pkgExpSlices, goVersion) if usage { resultExpSlices.Diagnostics = append(resultExpSlices.Diagnostics, diagnostic) } @@ -185,14 +184,14 @@ func (a *analyzer) run(pass *analysis.Pass) (any, error) { case *ast.FuncDecl: if node.Type.TypeParams != nil { for _, field := range node.Type.TypeParams.List { - a.detectConstraintsUsage(pass, field.Type, resultExpConstraints) + a.detectConstraintsUsage(pass, field.Type, resultExpConstraints, goVersion) } } case *ast.TypeSpec: if node.TypeParams != nil { for _, field := range node.TypeParams.List { - a.detectConstraintsUsage(pass, field.Type, resultExpConstraints) + a.detectConstraintsUsage(pass, field.Type, resultExpConstraints, goVersion) } } @@ -204,11 +203,11 @@ func (a *analyzer) run(pass *analysis.Pass) (any, error) { for _, method := range interfaceType.Methods.List { switch exp := method.Type.(type) { case *ast.BinaryExpr: - a.detectConstraintsUsage(pass, exp.X, resultExpConstraints) - a.detectConstraintsUsage(pass, exp.Y, resultExpConstraints) + a.detectConstraintsUsage(pass, exp.X, resultExpConstraints, goVersion) + a.detectConstraintsUsage(pass, exp.Y, resultExpConstraints, goVersion) case *ast.SelectorExpr: - a.detectConstraintsUsage(pass, exp, resultExpConstraints) + a.detectConstraintsUsage(pass, exp, resultExpConstraints, goVersion) } } } @@ -235,14 +234,14 @@ func (a *analyzer) run(pass *analysis.Pass) (any, error) { func (a *analyzer) detectPackageUsage(pass *analysis.Pass, replacements map[string]stdReplacement[*ast.CallExpr], selExpr *ast.SelectorExpr, ident *ast.Ident, callExpr *ast.CallExpr, - importPath string, + importPath string, goVersion int, ) (analysis.Diagnostic, bool) { rp, ok := replacements[selExpr.Sel.Name] if !ok { return analysis.Diagnostic{}, false } - if !a.skipGoVersionDetection && rp.MinGo > a.goVersion { + if !a.skipGoVersionDetection && rp.MinGo > goVersion { return analysis.Diagnostic{}, false } @@ -267,7 +266,7 @@ func (a *analyzer) detectPackageUsage(pass *analysis.Pass, return diagnostic, true } -func (a *analyzer) detectConstraintsUsage(pass *analysis.Pass, expr ast.Expr, result *Result) { +func (a *analyzer) detectConstraintsUsage(pass *analysis.Pass, expr ast.Expr, result *Result, goVersion int) { selExpr, ok := expr.(*ast.SelectorExpr) if !ok { return @@ -288,7 +287,7 @@ func (a *analyzer) detectConstraintsUsage(pass *analysis.Pass, expr ast.Expr, re return } - if !a.skipGoVersionDetection && rp.MinGo > a.goVersion { + if !a.skipGoVersionDetection && rp.MinGo > goVersion { result.shouldKeepImport = true return }