Compare commits

...

5 commits

Author SHA1 Message Date
dependabot[bot]
ea99373cd9
build(deps): bump golangci/golangci-lint-action
Bumps the github-actions group with 1 update: [golangci/golangci-lint-action](https://github.com/golangci/golangci-lint-action).


Updates `golangci/golangci-lint-action` from 6 to 7
- [Release notes](https://github.com/golangci/golangci-lint-action/releases)
- [Commits](https://github.com/golangci/golangci-lint-action/compare/v6...v7)

---
updated-dependencies:
- dependency-name: golangci/golangci-lint-action
  dependency-version: '7'
  dependency-type: direct:production
  update-type: version-update:semver-major
  dependency-group: github-actions
...

Signed-off-by: dependabot[bot] <support@github.com>
2025-04-02 17:08:31 +00:00
ecae7266e2 migrate to golangci-lint v2 and enable stricter linters 2025-04-02 19:07:19 +02:00
pre-commit-ci[bot]
134c7a8397 [pre-commit.ci] pre-commit autoupdate
updates:
- [github.com/golangci/golangci-lint: v1.64.7 → v2.0.2](https://github.com/golangci/golangci-lint/compare/v1.64.7...v2.0.2)
2025-04-02 19:07:19 +02:00
e5c81ff0dd update minimum go version 2025-04-02 18:39:55 +02:00
dependabot[bot]
a3efb8903a build(deps): bump golang.org/x/tools from 0.30.0 to 0.31.0
Bumps [golang.org/x/tools](https://github.com/golang/tools) from 0.30.0 to 0.31.0.
- [Release notes](https://github.com/golang/tools/releases)
- [Commits](https://github.com/golang/tools/compare/v0.30.0...v0.31.0)

---
updated-dependencies:
- dependency-name: golang.org/x/tools
  dependency-version: 0.31.0
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
2025-04-02 18:39:55 +02:00
8 changed files with 93 additions and 65 deletions

View file

@ -25,6 +25,6 @@ jobs:
with:
go-version: ${{ matrix.go }}
- name: golangci-lint
uses: golangci/golangci-lint-action@v6
uses: golangci/golangci-lint-action@v7
with:
version: latest

View file

@ -1,9 +1,18 @@
version: "2"
issues:
exclude-dirs:
- contrib
fix: true
linters:
default: all
disable:
- depguard
- exhaustruct
formatters:
enable:
- goimports
linters-settings:
goimports:
local-prefixes: "github.com/Crocmagnon/fatcontext"
- gofmt
- gofumpt
- golines
settings:
goimports:
local-prefixes:
- github.com/Crocmagnon/fatcontext

View file

@ -9,7 +9,7 @@ repos:
- id: end-of-file-fixer
- id: trailing-whitespace
- repo: https://github.com/golangci/golangci-lint
rev: v1.64.7
rev: v2.0.2
hooks:
- id: golangci-lint-full
- repo: local

View file

@ -1,3 +1,4 @@
// Package main runs the analyzer. It's the CLI entrypoint.
package main
import (

8
go.mod
View file

@ -1,10 +1,10 @@
module github.com/Crocmagnon/fatcontext
go 1.22.0
go 1.23.0
require golang.org/x/tools v0.30.0
require golang.org/x/tools v0.31.0
require (
golang.org/x/mod v0.23.0 // indirect
golang.org/x/sync v0.11.0 // indirect
golang.org/x/mod v0.24.0 // indirect
golang.org/x/sync v0.12.0 // indirect
)

12
go.sum
View file

@ -1,8 +1,8 @@
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
golang.org/x/mod v0.23.0 h1:Zb7khfcRGKk+kqfxFaP5tZqCnDZMjC5VtUBs87Hr6QM=
golang.org/x/mod v0.23.0/go.mod h1:6SkKJ3Xj0I0BrPOZoBy3bdMptDDU9oJrpohJ3eWZ1fY=
golang.org/x/sync v0.11.0 h1:GGz8+XQP4FvTTrjZPzNKTMFtSXH80RAzG+5ghFPgK9w=
golang.org/x/sync v0.11.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/tools v0.30.0 h1:BgcpHewrV5AUp2G9MebG4XPFI1E2W41zU1SaqVA9vJY=
golang.org/x/tools v0.30.0/go.mod h1:c347cR/OJfw5TI+GfX7RUPNMdDRRbjvYTS0jPyvsVtY=
golang.org/x/mod v0.24.0 h1:ZfthKaKaT4NrhGVZHO1/WDTwGES4De8KtWO0SIbNJMU=
golang.org/x/mod v0.24.0/go.mod h1:IXM97Txy2VM4PJ3gI61r1YEk/gAj6zAHN3AdZt6S9Ww=
golang.org/x/sync v0.12.0 h1:MHc5BpPuC30uJk597Ri8TV3CNZcTLu6B6z4lJy+g6Jw=
golang.org/x/sync v0.12.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA=
golang.org/x/tools v0.31.0 h1:0EedkvKDbh+qistFTd0Bcwe/YLh4vHwWEkiI0toFIBU=
golang.org/x/tools v0.31.0/go.mod h1:naFTU+Cev749tSJRXJlna0T3WxKvb1kWEx15xA4SdmQ=

View file

@ -1,3 +1,4 @@
// Package analyzer contains everything related to the linter analysis.
package analyzer
import (
@ -15,25 +16,31 @@ import (
"golang.org/x/tools/go/ast/inspector"
)
// FlagCheckStructPointers is a possible flag for the analyzer.
// Exported to make it usable in golangci-lint.
const FlagCheckStructPointers = "check-struct-pointers"
// NewAnalyzer returns a fatcontext analyzer.
func NewAnalyzer() *analysis.Analyzer {
r := &runner{}
rnnr := &runner{}
flags := flag.NewFlagSet("fatcontext", flag.ExitOnError)
flags.BoolVar(&r.DetectInStructPointers, FlagCheckStructPointers, false,
flags.BoolVar(&rnnr.DetectInStructPointers, FlagCheckStructPointers, false,
"set to true to detect potential fat contexts in struct pointers")
return &analysis.Analyzer{
Name: "fatcontext",
Doc: "detects nested contexts in loops and function literals",
Run: r.run,
Run: rnnr.run,
Flags: *flags,
Requires: []*analysis.Analyzer{inspect.Analyzer},
}
}
var errUnknown = errors.New("unknown node type")
var (
errUnknown = errors.New("unknown node type")
errInvalidAnalysis = errors.New("invalid analysis")
)
const (
categoryInLoop = "nested context in loop"
@ -47,7 +54,10 @@ type runner struct {
}
func (r *runner) run(pass *analysis.Pass) (interface{}, error) {
inspctr := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
inspctr, typeValid := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
if !typeValid {
return nil, errInvalidAnalysis
}
nodeFilter := []ast.Node{
(*ast.ForStmt)(nil),
@ -86,14 +96,18 @@ func (r *runner) run(pass *analysis.Pass) (interface{}, error) {
})
})
return nil, nil
return nil, nil //nolint:nilnil // we have no result to send to other analyzers
}
func (r *runner) shouldIgnoreReport(category string) bool {
return category == categoryInStructPointer && !r.DetectInStructPointers
}
func (r *runner) getSuggestedFixes(pass *analysis.Pass, assignStmt *ast.AssignStmt, category string) []analysis.SuggestedFix {
func (r *runner) getSuggestedFixes(
pass *analysis.Pass,
assignStmt *ast.AssignStmt,
category string,
) []analysis.SuggestedFix {
switch category {
case categoryInStructPointer, categoryUnsupported:
return nil
@ -160,31 +174,9 @@ func getBody(node ast.Node) (*ast.BlockStmt, error) {
func findNestedContext(pass *analysis.Pass, node ast.Node, stmts []ast.Stmt) *ast.AssignStmt {
for _, stmt := range stmts {
// Recurse if necessary
switch typedStmt := stmt.(type) {
case *ast.BlockStmt:
if found := findNestedContext(pass, node, typedStmt.List); found != nil {
return found
}
case *ast.IfStmt:
if found := findNestedContext(pass, node, typedStmt.Body.List); found != nil {
return found
}
case *ast.SwitchStmt:
if found := findNestedContext(pass, node, typedStmt.Body.List); found != nil {
return found
}
case *ast.CaseClause:
if found := findNestedContext(pass, node, typedStmt.Body); found != nil {
return found
}
case *ast.SelectStmt:
if found := findNestedContext(pass, node, typedStmt.Body.List); found != nil {
return found
}
case *ast.CommClause:
if found := findNestedContext(pass, node, typedStmt.Body); found != nil {
return found
}
stmtList := getStmtList(stmt)
if found := findNestedContext(pass, node, stmtList); found != nil {
return found
}
// Actually check for nested context
@ -226,28 +218,48 @@ func findNestedContext(pass *analysis.Pass, node ast.Node, stmts []ast.Stmt) *as
return nil
}
// render returns the pretty-print of the given node
func getStmtList(stmt ast.Stmt) []ast.Stmt {
switch typedStmt := stmt.(type) {
case *ast.BlockStmt:
return typedStmt.List
case *ast.IfStmt:
return typedStmt.Body.List
case *ast.SwitchStmt:
return typedStmt.Body.List
case *ast.CaseClause:
return typedStmt.Body
case *ast.SelectStmt:
return typedStmt.Body.List
case *ast.CommClause:
return typedStmt.Body
}
return nil
}
// render returns the pretty-print of the given node.
func render(fset *token.FileSet, x interface{}) ([]byte, error) {
var buf bytes.Buffer
if err := printer.Fprint(&buf, fset, x); err != nil {
return nil, fmt.Errorf("printing node: %w", err)
}
return buf.Bytes(), nil
}
func isContextFunction(exp ast.Expr, fnName ...string) bool {
call, ok := exp.(*ast.CallExpr)
if !ok {
call, typeValid := exp.(*ast.CallExpr)
if !typeValid {
return false
}
selector, ok := call.Fun.(*ast.SelectorExpr)
if !ok {
selector, typeValid := call.Fun.(*ast.SelectorExpr)
if !typeValid {
return false
}
ident, ok := selector.X.(*ast.Ident)
if !ok {
ident, typeValid := selector.X.(*ast.Ident)
if !typeValid {
return false
}
@ -275,16 +287,17 @@ func isWithinLoop(exp ast.Expr, node ast.Node, pass *analysis.Pass) bool {
func getRootIdent(pass *analysis.Pass, node ast.Node) *ast.Ident {
for {
switch n := node.(type) {
switch typedNode := node.(type) {
case *ast.Ident:
return n
return typedNode
case *ast.IndexExpr:
node = n.X
node = typedNode.X
case *ast.SelectorExpr:
if sel, ok := pass.TypesInfo.Selections[n]; ok && sel.Indirect() {
if sel, ok := pass.TypesInfo.Selections[typedNode]; ok && sel.Indirect() {
return nil // indirected (pointer) roots don't imply a (safe) copy
}
node = n.X
node = typedNode.X
default:
return nil
}
@ -292,9 +305,10 @@ func getRootIdent(pass *analysis.Pass, node ast.Node) *ast.Ident {
}
func isPointer(pass *analysis.Pass, exp ast.Node) bool {
switch n := exp.(type) {
switch n := exp.(type) { //nolint:gocritic // Future-proofing with switch instead of if.
case *ast.SelectorExpr:
sel, ok := pass.TypesInfo.Selections[n]
return ok && sel.Indirect()
}

View file

@ -9,6 +9,8 @@ import (
)
func TestAnalyzer(t *testing.T) {
t.Parallel()
testCases := []struct {
desc string
dir string
@ -42,21 +44,23 @@ func TestAnalyzer(t *testing.T) {
t.Run(test.desc+"_"+test.dir, func(t *testing.T) {
t.Parallel()
a := analyzer.NewAnalyzer()
anlzr := analyzer.NewAnalyzer()
for k, v := range test.options {
err := a.Flags.Set(k, v)
err := anlzr.Flags.Set(k, v)
if err != nil {
t.Fatal(err)
}
}
analysistest.RunWithSuggestedFixes(t, analysistest.TestData(), a, test.dir)
analysistest.RunWithSuggestedFixes(t, analysistest.TestData(), anlzr, test.dir)
})
}
}
func TestAnalyzer_cgo(t *testing.T) {
t.Parallel()
a := analyzer.NewAnalyzer()
analysistest.Run(t, analysistest.TestData(), a, "cgo")