Skip to content

Commit 22cdc68

Browse files
committed
refactor(lint): update and refactor golangci-lint
Signed-off-by: Minh Huy Tran <huy@perun.network>
1 parent 17ac78d commit 22cdc68

File tree

142 files changed

+1845
-1586
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

142 files changed

+1845
-1586
lines changed

.github/workflows/ci.yml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ on:
77
release:
88

99
env:
10-
go-version: 1.18
10+
go-version: 1.22
1111

1212
jobs:
1313
check-copyright:
1414
name: Copyright
1515
runs-on: ubuntu-latest
1616
steps:
1717
- name: Checkout
18-
uses: actions/checkout@v2
18+
uses: actions/checkout@v4
1919
with:
2020
fetch-depth: 0
2121
- name: Authors
@@ -26,20 +26,20 @@ jobs:
2626
runs-on: ubuntu-latest
2727
steps:
2828
- name: Checkout
29-
uses: actions/checkout@v2
29+
uses: actions/checkout@v4
3030

3131
- name: Check vanity import
3232
run: .scripts/check-vanity-imports.sh $GITHUB_WORKSPACE
3333

3434
- name: Setup Go
35-
uses: actions/setup-go@v2
35+
uses: actions/setup-go@v4
3636
with:
3737
go-version: ${{ env.go-version }}
3838

3939
- name: golangci-lint
40-
uses: golangci/golangci-lint-action@v3
40+
uses: golangci/golangci-lint-action@v7
4141
with:
42-
version: v1.45
42+
version: v2.1
4343

4444
- name: Lint proto files
4545
uses: plexsystems/protolint-action@v0.6.0

.golangci.json

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
{
2+
"formatters": {
3+
"enable": [
4+
"gofmt",
5+
"gofumpt",
6+
"goimports"
7+
],
8+
"exclusions": {
9+
"generated": "lax",
10+
"paths": [
11+
"third_party$",
12+
"builtin$",
13+
"examples$"
14+
]
15+
}
16+
},
17+
"linters": {
18+
"default": "all",
19+
"disable": [
20+
"err113",
21+
"errorlint",
22+
"gochecknoglobals",
23+
"gochecknoinits",
24+
"ireturn",
25+
"lll",
26+
"nlreturn",
27+
"paralleltest",
28+
"promlinter",
29+
"tparallel",
30+
"varnamelen",
31+
"wrapcheck",
32+
"wsl",
33+
"testifylint",
34+
"revive",
35+
"recvcheck",
36+
"prealloc",
37+
"depguard",
38+
"nonamedreturns",
39+
"inamedparam",
40+
"exhaustruct"
41+
],
42+
"exclusions": {
43+
"generated": "lax",
44+
"paths": [
45+
"third_party$",
46+
"builtin$",
47+
"examples$"
48+
],
49+
"presets": [
50+
"common-false-positives",
51+
"legacy",
52+
"std-error-handling"
53+
],
54+
"rules": [
55+
{
56+
"linters": [
57+
"err113",
58+
"forcetypeassert",
59+
"funlen"
60+
],
61+
"path": "test"
62+
},
63+
{
64+
"linters": [
65+
"revive"
66+
],
67+
"path": "test",
68+
"text": "context-as-argument"
69+
}
70+
]
71+
},
72+
"settings": {
73+
"cyclop": {
74+
"max-complexity": 15
75+
},
76+
"forbidigo": {
77+
"forbid": [
78+
{
79+
"pattern": "^[Ee]quals$"
80+
},
81+
{
82+
"pattern": "^print.*$"
83+
},
84+
{
85+
"pattern": "fmt\\.Print.*"
86+
}
87+
]
88+
},
89+
"goheader": {
90+
"template-path": ".scripts/copyright-notice",
91+
"values": {
92+
"regexp": {
93+
"ANY_YEAR": "20(19|2\\d)"
94+
}
95+
}
96+
}
97+
}
98+
},
99+
"run": {
100+
"modules-download-mode": "readonly"
101+
},
102+
"version": "2"
103+
}

.golangci.yml

Lines changed: 0 additions & 81 deletions
This file was deleted.

apps/payment/app_internal_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func TestApp_ValidInit(t *testing.T) {
4242
assert.Panics(func() { app.ValidInit(nil, wrongdata) }) //nolint:errcheck
4343

4444
data := &channel.State{Data: Data()}
45-
assert.Nil(app.ValidInit(nil, data))
45+
assert.NoError(app.ValidInit(nil, data))
4646
}
4747

4848
func TestApp_ValidTransition(t *testing.T) {
@@ -93,9 +93,9 @@ func TestApp_ValidTransition(t *testing.T) {
9393
test.WithBalances(asBalances(tt.from...)...),
9494
)
9595
numParticipants := len(tt.from[0])
96-
for i := 0; i < numParticipants; i++ {
96+
for i := range numParticipants {
9797
// valid self-transition
98-
assert.NoError(app.ValidTransition(nil, from, from, channel.Index(i)))
98+
assert.NoError(app.ValidTransition(nil, from, from, channel.Index(i))) //nolint:gosec
9999
}
100100

101101
for _, tto := range tt.tos {
@@ -104,8 +104,8 @@ func TestApp_ValidTransition(t *testing.T) {
104104
test.WithAppData(Data()),
105105
test.WithBalances(asBalances(tto.alloc...)...),
106106
)
107-
for i := 0; i < numParticipants; i++ {
108-
err := app.ValidTransition(nil, from, to, channel.Index(i))
107+
for i := range numParticipants {
108+
err := app.ValidTransition(nil, from, to, channel.Index(i)) //nolint:gosec
109109
if i == tto.valid {
110110
assert.NoError(err)
111111
} else {

apps/payment/randomizer_internal_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"testing"
1919

2020
"github.com/stretchr/testify/assert"
21+
"github.com/stretchr/testify/require"
2122

2223
_ "perun.network/go-perun/backend/sim" // backend init
2324
"perun.network/go-perun/channel"
@@ -31,7 +32,7 @@ func TestRandomizer(t *testing.T) {
3132
app := r.NewRandomApp(rng, channel.TestBackendID)
3233
channel.RegisterApp(app)
3334
regApp, err := channel.Resolve(app.Def())
34-
assert.NoError(t, err)
35+
require.NoError(t, err)
3536
assert.True(t, app.Def().Equal(regApp.Def()))
3637
assert.True(t, IsData(r.NewRandomData(rng)))
3738
}

apps/payment/resolver_internal_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func TestResolver(t *testing.T) {
3636
channel.RegisterAppResolver(def.Equal, &Resolver{})
3737

3838
app, err := channel.Resolve(def)
39-
assert.NoError(err)
39+
require.NoError(err)
4040
require.NotNil(app)
4141
assert.True(def.Equal(app.Def()))
4242
}
@@ -47,7 +47,7 @@ func TestData(t *testing.T) {
4747
assert.NotPanics(func() {
4848
data := Data()
4949
_, err := data.MarshalBinary()
50-
assert.Nil(err)
50+
assert.NoError(err)
5151
})
5252

5353
assert.NotPanics(func() {

backend/sim/channel/app.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ type AppID struct {
2626
*wallet.Address
2727
}
2828

29+
// NewRandomAppID generates a new random app identifier.
30+
func NewRandomAppID(rng *rand.Rand) AppID {
31+
addr := wallet.NewRandomAddress(rng)
32+
return AppID{Address: addr}
33+
}
34+
2935
// Equal returns whether the object is equal to the given object.
3036
func (id AppID) Equal(b channel.AppID) bool {
3137
bTyped, ok := b.(AppID)
@@ -44,9 +50,3 @@ func (id AppID) Key() channel.AppIDKey {
4450
}
4551
return channel.AppIDKey(b)
4652
}
47-
48-
// NewRandomAppID generates a new random app identifier.
49-
func NewRandomAppID(rng *rand.Rand) AppID {
50-
addr := wallet.NewRandomAddress(rng)
51-
return AppID{Address: addr}
52-
}

backend/sim/channel/asset.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@ package channel
1717
import (
1818
"encoding/binary"
1919
"fmt"
20+
"math"
2021
"math/rand"
2122

23+
"github.com/pkg/errors"
2224
"perun.network/go-perun/channel"
2325
)
2426

@@ -44,16 +46,23 @@ func NewRandomAsset(rng *rand.Rand) *Asset {
4446
// MarshalBinary marshals the address into its binary representation.
4547
func (a Asset) MarshalBinary() ([]byte, error) {
4648
data := make([]byte, assetLen)
49+
if a.ID < 0 {
50+
return nil, errors.New("asset ID must be non-negative")
51+
}
4752
byteOrder.PutUint64(data, uint64(a.ID))
4853
return data, nil
4954
}
5055

5156
// UnmarshalBinary unmarshals the asset from its binary representation.
5257
func (a *Asset) UnmarshalBinary(data []byte) error {
5358
if len(data) != assetLen {
54-
return fmt.Errorf("unexpected length %d, want %d", len(data), assetLen) //nolint:goerr113 // We do not want to define this as constant error.
59+
return fmt.Errorf("unexpected length %d, want %d", len(data), assetLen) // We do not want to define this as constant error.
60+
}
61+
id := byteOrder.Uint64(data)
62+
if id > math.MaxInt64 {
63+
return fmt.Errorf("asset ID %d is too large", id)
5564
}
56-
a.ID = int64(byteOrder.Uint64(data))
65+
a.ID = int64(id)
5766
return nil
5867
}
5968

backend/sim/channel/asset_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import (
2424

2525
func Test_Asset_GenericMarshaler(t *testing.T) {
2626
rng := pkgtest.Prng(t)
27-
for n := 0; n < 10; n++ {
27+
for range 10 {
2828
test.GenericMarshalerTest(t, &channel.Asset{ID: rng.Int63()})
2929
}
3030
}

0 commit comments

Comments
 (0)