diff --git a/.github/workflows/reviewdog.yml b/.github/workflows/reviewdog.yml index 8987c51d..dd3fc059 100644 --- a/.github/workflows/reviewdog.yml +++ b/.github/workflows/reviewdog.yml @@ -1,22 +1,20 @@ name: reviewdog on: [pull_request] jobs: - staticcheck: - name: runner / staticcheck + golangci-lint: runs-on: ubuntu-latest + name: runner / golangci-lint steps: - - uses: actions/checkout@v2 - - name: disable demo lint check - run: | - rm -rf tars/tools/Demo* - - uses: reviewdog/action-staticcheck@v1 + - name: Check out code into the Go module directory + uses: actions/checkout@v3 + - name: golangci-lint + uses: reviewdog/action-golangci-lint@v2 with: github_token: ${{ secrets.github_token }} # Change reviewdog reporter if you need [github-pr-check,github-check,github-pr-review]. reporter: github-pr-review # Report all results. filter_mode: nofilter - # Exit with 1 when it find at least one finding. + # Exit with 1 when it finds at least one finding. fail_on_error: true - # Set staticcheck flags - staticcheck_flags: -checks=inherit,-SA1019,-SA4001 + #golangci_lint_flags \ No newline at end of file diff --git a/.golangci.yaml b/.golangci.yaml new file mode 100644 index 00000000..28a17047 --- /dev/null +++ b/.golangci.yaml @@ -0,0 +1,29 @@ +run: + timeout: 30m + skip-dirs: + - tars/protocol/res + +linters: + disable-all: true + enable: + - unused + - ineffassign + - goimports + - gofmt + - misspell + - unparam + - unconvert + - govet + # - errcheck + - staticcheck + +linters-settings: + staticcheck: + go: "1.17" + checks: + - "all" + # TODO(fix) Using a deprecated function, variable, constant or field + - "-SA1019" + + unused: + go: "1.17" diff --git a/tars/protocol/codec/codec.go b/tars/protocol/codec/codec.go index 6c48c215..86460de6 100755 --- a/tars/protocol/codec/codec.go +++ b/tars/protocol/codec/codec.go @@ -385,6 +385,7 @@ func (b *Reader) unreadHead(curTag byte) { } // Next return the []byte of next n . +// //go:nosplit func (b *Reader) Next(n int) []byte { if n <= 0 { @@ -397,6 +398,7 @@ func (b *Reader) Next(n int) []byte { } // Skip the next n byte. +// //go:nosplit func (b *Reader) Skip(n int) { if n <= 0 { diff --git a/tars/protocol/tarsprotocol.go b/tars/protocol/tarsprotocol.go index 60f7e08e..a3909586 100755 --- a/tars/protocol/tarsprotocol.go +++ b/tars/protocol/tarsprotocol.go @@ -2,6 +2,7 @@ package protocol import ( "encoding/binary" + "github.com/TarsCloud/TarsGo/tars/protocol/codec" "github.com/TarsCloud/TarsGo/tars/protocol/res/requestf" ) diff --git a/tars/protocol/tarsprotocol_test.go b/tars/protocol/tarsprotocol_test.go index f6cfc30f..5ac5d3c9 100644 --- a/tars/protocol/tarsprotocol_test.go +++ b/tars/protocol/tarsprotocol_test.go @@ -3,11 +3,12 @@ package protocol import ( "bytes" "encoding/binary" + "testing" + "github.com/TarsCloud/TarsGo/tars/protocol/codec" "github.com/TarsCloud/TarsGo/tars/protocol/res/basef" "github.com/TarsCloud/TarsGo/tars/protocol/res/requestf" "github.com/stretchr/testify/assert" - "testing" ) func TestSetMaxPackageLength(t *testing.T) { diff --git a/tars/protocol/tup/tup.go b/tars/protocol/tup/tup.go index 3f01bf16..71400cb0 100644 --- a/tars/protocol/tup/tup.go +++ b/tars/protocol/tup/tup.go @@ -30,7 +30,7 @@ func (u *UniAttribute) PutBuffer(k string, buf []byte) { func (u *UniAttribute) GetBuffer(k string, buf *[]byte) error { var ( err error - ok = false + ok bool ) if *buf, ok = u._data[k]; !ok { err = fmt.Errorf("tup get error: donot find key: %s", k) diff --git a/tars/selector/roundrobin/round_robin_test.go b/tars/selector/roundrobin/round_robin_test.go index 344a4a0c..c9750abf 100644 --- a/tars/selector/roundrobin/round_robin_test.go +++ b/tars/selector/roundrobin/round_robin_test.go @@ -1,8 +1,9 @@ package roundrobin import ( - "github.com/TarsCloud/TarsGo/tars/util/endpoint" "testing" + + "github.com/TarsCloud/TarsGo/tars/util/endpoint" ) func TestRoundRobin(t *testing.T) { diff --git a/tars/selector/selector.go b/tars/selector/selector.go index db50da82..151bedbe 100644 --- a/tars/selector/selector.go +++ b/tars/selector/selector.go @@ -1,9 +1,10 @@ package selector import ( - "github.com/TarsCloud/TarsGo/tars/util/endpoint" "math" "sort" + + "github.com/TarsCloud/TarsGo/tars/util/endpoint" ) // HashType is the hash type @@ -48,7 +49,8 @@ type Selector interface { } func BuildStaticWeightList(endpoints []endpoint.Endpoint) []int { - var maxRange, totalWeight, totalCapacity, minWeight, maxWeight = 0, 0, 0, math.MaxInt32, math.MinInt32 + var maxRange, totalWeight, totalCapacity int + minWeight, maxWeight := math.MaxInt32, math.MinInt32 for _, node := range endpoints { if endpoint.WeightType(node.WeightType) != endpoint.EStaticWeight { return nil diff --git a/tars/util/conf/conf.go b/tars/util/conf/conf.go index 27eb78e5..8fb53ff2 100644 --- a/tars/util/conf/conf.go +++ b/tars/util/conf/conf.go @@ -43,9 +43,9 @@ func (e *elem) setValue(value string) *elem { return e } -func (e *elem) addChild(name string, child *elem) *elem { +func (e *elem) addChild(name string, child *elem) { e.children[name] = child - return e + return } func (e *elem) addLine(line string) *elem { @@ -158,7 +158,7 @@ func (e *elem) getMap(path string) (map[string]string, error) { kvMap := make(map[string]string) targetNode, err := e.getElem(pathVec) if err != nil { - return kvMap, nil + return kvMap, err } for _, child := range targetNode.children { if child.isLeaf() { diff --git a/tars/util/grace/signal_windows.go b/tars/util/grace/signal_windows.go index 00f7ae19..11a47cd2 100644 --- a/tars/util/grace/signal_windows.go +++ b/tars/util/grace/signal_windows.go @@ -1,3 +1,4 @@ +//go:build windows // +build windows package grace diff --git a/tars/util/rtimer/timewheel_test.go b/tars/util/rtimer/timewheel_test.go index 8c12a1d8..14a90b71 100644 --- a/tars/util/rtimer/timewheel_test.go +++ b/tars/util/rtimer/timewheel_test.go @@ -16,7 +16,7 @@ func TestTimeWheel(t *testing.T) { wg := sync.WaitGroup{} for i := 0; i < 100; i++ { wg.Add(1) - go func(i int) { + go func() { start := time.Now().UnixNano() <-After(sleepTime) end := time.Now().UnixNano() @@ -27,7 +27,7 @@ func TestTimeWheel(t *testing.T) { deviation -= d } wg.Done() - }(i) + }() time.Sleep(time.Millisecond * 100) } wg.Wait() diff --git a/tars/util/sync/once.go b/tars/util/sync/once.go index 7fca09bd..b13e883b 100644 --- a/tars/util/sync/once.go +++ b/tars/util/sync/once.go @@ -24,7 +24,9 @@ type Once struct { // Do calls the function f if and only if Do is being called for the // first time for this instance of Once. In other words, given -// var once Once +// +// var once Once +// // if once.Do(f) is called multiple times, only the first call will invoke f, // even if f has a different value in each invocation. A new instance of // Once is required for each function to execute. @@ -32,14 +34,14 @@ type Once struct { // Do is intended for initialization that must be run exactly once. Since f // is niladic, it may be necessary to use a function literal to capture the // arguments to a function to be invoked by Do: -// config.once.Do(func() { config.init(filename) }) +// +// config.once.Do(func() { config.init(filename) }) // // Because no call to Do returns until the one call to f returns, if f causes // Do to be called, it will deadlock. // // If f panics, Do considers it to have returned; future calls of Do return // without calling f. -// func (o *Once) Do(f func() error) error { // Note: Here is an incorrect implementation of Do: //