Skip to content

Commit

Permalink
Merge pull request #19 from ldez/fix/go-version
Browse files Browse the repository at this point in the history
fix: Go version
  • Loading branch information
sonatard committed Sep 15, 2024
2 parents ce7f5c0 + 676dd6a commit 201254a
Show file tree
Hide file tree
Showing 12 changed files with 96 additions and 24 deletions.
8 changes: 6 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
name: CI

on:
push:
branches:
- master
pull_request:

jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
go: [ '1.23', '1.22' ]
go: [ stable, oldstable ]
name: Go ${{ matrix.go }} test
steps:
- uses: actions/checkout@v4
Expand All @@ -19,7 +23,7 @@ jobs:
- run: make lint
- run: make test_coverage
- name: Upload code coverage to codecov
if: matrix.go == '1.23'
if: matrix.go == 'stable'
uses: codecov/codecov-action@v3
with:
file: ./coverage.out
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: 1.x
go-version: stable
- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v6
with:
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
coverage.out
/noctx
52 changes: 38 additions & 14 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,38 @@
run:
linters-settings:
govet:
enable-all: true
linters:
enable-all: true
disable:
- gochecknoglobals
- gomnd
- gocognit
- nestif
- nilnil
- paralleltest
- varnamelen
linters:
enable-all: true
disable:
- execinquery # deprecated
- exportloopref # deprecated
- gomnd # deprecated
- gochecknoglobals
- exhaustruct
- mnd
- gocognit
- nestif
- nilnil
- paralleltest
- varnamelen

linters-settings:
govet:
enable-all: true
perfsprint:
err-error: true
errorf: true
sprintf1: true
strconcat: false
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

output:
show-stats: true
sort-results: true
sort-order:
- linter
- file
37 changes: 37 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
version: 2
project_name: noctx

builds:
- binary: noctx

main: ./cmd/noctx/main.go
env:
- CGO_ENABLED=0
flags:
- -trimpath
goos:
- windows
- darwin
- linux
goarch:
- amd64
- 386
- arm
- arm64
goarm:
- 7
- 6
- 5
ignore:
- goos: darwin
goarch: 386

archives:
- id: noctx
name_template: '{{ .ProjectName }}_v{{ .Version }}_{{ .Os }}_{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}{{ if .Mips }}_{{ .Mips }}{{ end }}'
format: tar.gz
format_overrides:
- goos: windows
format: zip
files:
- LICENSE
6 changes: 4 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.PHONY: all imports test lint
.PHONY: all imports test lint build

all: imports test lint
all: imports test lint build

imports:
goimports -w ./
Expand All @@ -14,3 +14,5 @@ test_coverage:
lint:
golangci-lint run ./...

build:
go build -ldflags "-s -w" -trimpath ./cmd/noctx/
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,11 @@ linters:
$ golangci-lint run

# Only noctx execute
golangci-lint run --disable-all -E noctx
golangci-lint run --enable-only noctx
```

## Detection rules

- Executing following functions
- `net/http.Get`
- `net/http.Head`
Expand All @@ -68,6 +69,7 @@ golangci-lint run --disable-all -E noctx
- `http.Request` returned by `http.NewRequest` function and passes it to other function.

## How to fix

- Send http request using `(*http.Client).Do(*http.Request)` method.
- In Go 1.13 and later, use `http.NewRequestWithContext` function instead of using `http.NewRequest` function.
- In Go 1.12 and earlier, call `(http.Request).WithContext(ctx)` after `http.NewRequest`.
Expand Down Expand Up @@ -168,6 +170,7 @@ func main() {
```

## Reference

- [net/http - NewRequest](https://golang.org/pkg/net/http/#NewRequest)
- [net/http - NewRequestWithContext](https://golang.org/pkg/net/http/#NewRequestWithContext)
- [net/http - Request.WithContext](https://golang.org/pkg/net/http/#Request.WithContext)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/sonatard/noctx

go 1.22.5
go 1.22.0

require (
github.com/gostaticanalysis/analysisutil v0.7.1
Expand Down
1 change: 1 addition & 0 deletions ngfunc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func ngCalledFuncs(pass *analysis.Pass, ngFuncs []*types.Func) []*Report {
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 {
Expand Down
4 changes: 2 additions & 2 deletions ngfunc/types.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package ngfunc

import (
"fmt"
"errors"
"go/types"
"strings"

"github.com/gostaticanalysis/analysisutil"
"golang.org/x/tools/go/analysis"
)

var errNotFound = fmt.Errorf("function not found")
var errNotFound = errors.New("function not found")

func typeFuncs(pass *analysis.Pass, funcs []string) []*types.Func {
fs := make([]*types.Func, 0, len(funcs))
Expand Down
1 change: 1 addition & 0 deletions noctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package noctx

import (
"fmt"

"github.com/sonatard/noctx/ngfunc"
"github.com/sonatard/noctx/reqwithoutctx"
"golang.org/x/tools/go/analysis"
Expand Down
1 change: 0 additions & 1 deletion reqwithoutctx/ssa.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"go/types"

"github.com/gostaticanalysis/analysisutil"

"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/passes/buildssa"
"golang.org/x/tools/go/ssa"
Expand Down

0 comments on commit 201254a

Please sign in to comment.