diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index a079bb4..377d42c 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -6,21 +6,21 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - go: [ '1.14', '1.13' ] + go: [ '1.20', '1.19' ] name: Go ${{ matrix.go }} test steps: - name: Checkout repository - uses: actions/checkout@v2 + uses: actions/checkout@v3 - name: Setup Go - uses: actions/setup-go@v2 + uses: actions/setup-go@v3 with: go-version: ${{ matrix.go }} - name: Install GolangCI-Lint - run: curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.27.0 + run: curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.51.2 - run: make test_coverage - name: Upload code coverage to codecov - if: matrix.go == '1.14' - uses: codecov/codecov-action@v1 + if: matrix.go == '1.20' + uses: codecov/codecov-action@v3 with: file: ./coverage.out - run: make lint diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 58c6ed0..bd9e3c2 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -14,11 +14,11 @@ jobs: - name: Unshallow run: git fetch --prune --unshallow - name: Set up Go - uses: actions/setup-go@v2 + uses: actions/setup-go@v3 with: go-version: 1.x - name: Run GoReleaser - uses: goreleaser/goreleaser-action@v1 + uses: goreleaser/goreleaser-action@v4 with: version: latest args: release --rm-dist diff --git a/.golangci.yml b/.golangci.yml index 1580acd..55ebeeb 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -1,20 +1,14 @@ run: - -linters-settings: - govet: + linters-settings: + govet: + enable-all: true + linters: enable-all: true - -linters: - enable-all: true - disable: - - gochecknoglobals - - gomnd - - gocognit - - nestif - -issues: - exclude-rules: - - path: reqwithoutctx/ssa.go - text: "Consider preallocating `exts`" - linters: - - prealloc + disable: + - gochecknoglobals + - gomnd + - gocognit + - nestif + - nilnil + - paralleltest + - varnamelen \ No newline at end of file diff --git a/go.mod b/go.mod index 7df59cb..7a71f3b 100644 --- a/go.mod +++ b/go.mod @@ -1,8 +1,14 @@ module github.com/sonatard/noctx -go 1.16 +go 1.20 require ( github.com/gostaticanalysis/analysisutil v0.7.1 - golang.org/x/tools v0.1.4 + golang.org/x/tools v0.7.0 +) + +require ( + github.com/gostaticanalysis/comment v1.4.2 // indirect + golang.org/x/mod v0.9.0 // indirect + golang.org/x/sys v0.6.0 // indirect ) diff --git a/go.sum b/go.sum index 6150589..e85de61 100644 --- a/go.sum +++ b/go.sum @@ -20,29 +20,24 @@ github.com/tenntenn/modver v1.0.1/go.mod h1:bePIyQPb7UeioSRkw3Q0XeMhYZSMx9B8ePqg github.com/tenntenn/text/transform v0.0.0-20200319021203-7eef512accb3 h1:f+jULpRQGxTSkNYKJ51yaw6ChIqO+Je8UqsTKN/cDag= github.com/tenntenn/text/transform v0.0.0-20200319021203-7eef512accb3/go.mod h1:ON8b8w4BN/kE1EOhwT0o+d62W65a6aPw1nouo9LMgyY= github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= -github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/mod v0.4.1/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= -golang.org/x/mod v0.4.2 h1:Gz96sIWK3OalVv/I/qNygP42zyoKp3xptRVCWRFEBvo= -golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/mod v0.9.0 h1:KENHtAZL2y3NLMYZeHY9DW8HW8V+kQyJsY/V9JlKvCs= +golang.org/x/mod v0.9.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= -golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210510120138-977fb7262007 h1:gG67DSER+11cZvqIMb8S8bt0vZtiN6xWYARwirrOSfE= -golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= golang.org/x/text v0.3.3 h1:cokOdA+Jmi5PJGXLlLllQSgYigAEfHXJAERHVMaCc2k= @@ -51,10 +46,9 @@ golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGm golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.1.1-0.20210205202024-ef80cdb6ec6d/go.mod h1:9bzcO0MWcOuT0tm1iBGzDVPshzfwoVvREIui8C+MHqU= golang.org/x/tools v0.1.1-0.20210302220138-2ac05c832e1a/go.mod h1:9bzcO0MWcOuT0tm1iBGzDVPshzfwoVvREIui8C+MHqU= -golang.org/x/tools v0.1.4 h1:cVngSRcfgyZCzys3KYOpCFa+4dqX/Oub9tAq00ttGVs= -golang.org/x/tools v0.1.4/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= +golang.org/x/tools v0.7.0 h1:W4OVu8VVOaIO0yzWMNdepAulS7YfoS3Zabrm8DOXXU4= +golang.org/x/tools v0.7.0/go.mod h1:4pg6aUX35JBAogB10C9AtvVL+qowtN4pT3CGSQex14s= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= diff --git a/ngfunc/main.go b/ngfunc/main.go index cfeb0f0..4630621 100644 --- a/ngfunc/main.go +++ b/ngfunc/main.go @@ -1,6 +1,7 @@ package ngfunc import ( + "fmt" "go/types" "github.com/gostaticanalysis/analysisutil" @@ -34,8 +35,11 @@ func Run(pass *analysis.Pass) (interface{}, error) { func ngCalledFuncs(pass *analysis.Pass, ngFuncs []*types.Func) []*Report { var reports []*Report - srcFuncs := pass.ResultOf[buildssa.Analyzer].(*buildssa.SSA).SrcFuncs - for _, sf := range srcFuncs { + ssa, ok := pass.ResultOf[buildssa.Analyzer].(*buildssa.SSA) + if !ok { + panic(fmt.Sprintf("%T is not *buildssa.SSA", pass.ResultOf[buildssa.Analyzer])) + } + for _, sf := range ssa.SrcFuncs { for _, b := range sf.Blocks { for _, instr := range b.Instrs { for _, ngFunc := range ngFuncs { diff --git a/noctx.go b/noctx.go index 478ad88..89e0446 100644 --- a/noctx.go +++ b/noctx.go @@ -1,6 +1,7 @@ package noctx import ( + "fmt" "github.com/sonatard/noctx/ngfunc" "github.com/sonatard/noctx/reqwithoutctx" "golang.org/x/tools/go/analysis" @@ -8,23 +9,26 @@ import ( ) var Analyzer = &analysis.Analyzer{ - Name: "noctx", - Doc: Doc, - Run: run, + Name: "noctx", + Doc: Doc, + Run: run, + RunDespiteErrors: false, Requires: []*analysis.Analyzer{ buildssa.Analyzer, }, + ResultType: nil, + FactTypes: nil, } const Doc = "noctx finds sending http request without context.Context" func run(pass *analysis.Pass) (interface{}, error) { if _, err := ngfunc.Run(pass); err != nil { - return nil, err + return nil, fmt.Errorf("run: %w", err) } if _, err := reqwithoutctx.Run(pass); err != nil { - return nil, err + return nil, fmt.Errorf("run: %w", err) } return nil, nil diff --git a/reqwithoutctx/ssa.go b/reqwithoutctx/ssa.go index 3575126..d7e0f50 100644 --- a/reqwithoutctx/ssa.go +++ b/reqwithoutctx/ssa.go @@ -1,6 +1,7 @@ package reqwithoutctx import ( + "fmt" "go/types" "github.com/gostaticanalysis/analysisutil" @@ -10,6 +11,7 @@ import ( "golang.org/x/tools/go/ssa" ) +//nolint:govet type Analyzer struct { Funcs []*ssa.Function newRequestType types.Type @@ -20,10 +22,13 @@ func NewAnalyzer(pass *analysis.Pass) *Analyzer { newRequestType := analysisutil.TypeOf(pass, "net/http", "NewRequest") requestType := analysisutil.TypeOf(pass, "net/http", "*Request") - srcFuncs := pass.ResultOf[buildssa.Analyzer].(*buildssa.SSA).SrcFuncs + ssa, ok := pass.ResultOf[buildssa.Analyzer].(*buildssa.SSA) + if !ok { + panic(fmt.Sprintf("%T is not *buildssa.SSA", pass.ResultOf[buildssa.Analyzer])) + } return &Analyzer{ - Funcs: srcFuncs, + Funcs: ssa.SrcFuncs, newRequestType: newRequestType, requestType: requestType, } @@ -88,14 +93,14 @@ func (a *Analyzer) usedReqs() map[string]*ssa.Extract { } func (a *Analyzer) usedReqByCall(call *ssa.Call) []*ssa.Extract { - var exts []*ssa.Extract + args := call.Common().Args + exts := make([]*ssa.Extract, 0, len(args)) // skip net/http.Request method call if call.Common().Signature().Recv() != nil && types.Identical(call.Value().Type(), a.requestType) { return exts } - args := call.Common().Args if len(args) == 0 { return exts }