From 70e6ad2141f5356f171e6ce155b64e72ab7febf5 Mon Sep 17 00:00:00 2001 From: sivchari Date: Sat, 25 Mar 2023 00:34:24 +0900 Subject: [PATCH] update: improve static check --- .github/workflows/ci.yml | 14 +++++++------- containedctx.go | 11 +---------- plugin/main.go | 2 +- testdata/src/a/b.go | 7 +++++++ 4 files changed, 16 insertions(+), 18 deletions(-) create mode 100644 testdata/src/a/b.go diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 8e47f29..ccdae91 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -12,17 +12,17 @@ jobs: runs-on: ubuntu-latest steps: - - name: Set up Go 1.17 - uses: actions/setup-go@v1 + - name: Set up Go 1.20 + uses: actions/setup-go@v4 with: - go-version: 1.17 + go-version: '1.20' id: go - name: Check out code into the Go module directory - uses: actions/checkout@v2 + uses: actions/checkout@v3 - name: Restore cache - uses: actions/cache@v1 + uses: actions/cache@v3 with: path: ~/go/pkg/mod key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} @@ -38,8 +38,8 @@ jobs: name: Lint runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - name: golangci-lint uses: golangci/golangci-lint-action@v2 with: - version: v1.43.0 + version: v1.52.1 diff --git a/containedctx.go b/containedctx.go index 5a2c2da..0260d6a 100644 --- a/containedctx.go +++ b/containedctx.go @@ -34,16 +34,7 @@ func run(pass *analysis.Pass) (interface{}, error) { return } for _, field := range structTyp.Fields.List { - selectorExpr, ok := field.Type.(*ast.SelectorExpr) - if !ok { - continue - } - xname, ok := selectorExpr.X.(*ast.Ident) - if !ok { - continue - } - selname := selectorExpr.Sel.Name - if xname.Name+"."+selname == "context.Context" { + if pass.TypesInfo.TypeOf(field.Type).String() == "context.Context" { pass.Reportf(field.Pos(), "found a struct that contains a context.Context field") } } diff --git a/plugin/main.go b/plugin/main.go index 5eed04c..9393372 100644 --- a/plugin/main.go +++ b/plugin/main.go @@ -14,7 +14,7 @@ import ( // flags for Analyzer.Flag. // If you would like to specify flags for your plugin, you can put them via 'ldflags' as below. -// $ go build -buildmode=plugin -ldflags "-X 'main.flags=-opt val'" containedctx/plugin/containedctx +// $ go build -buildmode=plugin -ldflags "-X 'main.flags=-opt val'" containedctx/plugin/containedctx var flags string // AnalyzerPlugin provides analyzers as a plugin. diff --git a/testdata/src/a/b.go b/testdata/src/a/b.go new file mode 100644 index 0000000..d76a3cc --- /dev/null +++ b/testdata/src/a/b.go @@ -0,0 +1,7 @@ +package a + +import xcontext "context" + +type namedng struct { + ctx xcontext.Context // want "found a struct that contains a context.Context field" +}