Skip to content

Commit

Permalink
feat: use range in for loops (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
sergeii authored Jul 12, 2024
1 parent 726d791 commit 36d7a5b
Show file tree
Hide file tree
Showing 14 changed files with 15 additions and 18 deletions.
1 change: 0 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ linters:
- testifylint
- perfsprint
- contextcheck
- intrange
- revive
linters-settings:
cyclop:
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ For other tags see [container registry][packages].
Alternatively you can download and run a server binary suitable for your platform from one of the [releases][releases].

## Building from source
To build the project from source you need Go 1.21+
To build the project from source you need Go 1.22+
```
go build -o swat4master cmd/swat4master/main.go
```
Expand Down
2 changes: 1 addition & 1 deletion cmd/swat4master/modules/prober/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func NewWorkerGroup(
func (wg *WorkerGroup) Run(ctx context.Context) chan probe.Probe {
wg.metrics.DiscoveryWorkersAvailable.Add(float64(wg.concurrency))
queue := make(chan probe.Probe, wg.concurrency)
for i := 0; i < wg.concurrency; i++ {
for range wg.concurrency {
go wg.work(ctx, queue)
}
return queue
Expand Down
4 changes: 2 additions & 2 deletions internal/persistence/memory/probes/probes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ func TestProbesMemoryRepo_Pop(t *testing.T) {

<-started
// advance 100ms in steps
for i := 0; i < 100; i++ {
for range 100 {
c.Advance(time.Millisecond * 1)
advanced <- true
}
Expand Down Expand Up @@ -343,7 +343,7 @@ func TestProbesMemoryRepo_PopAny(t *testing.T) {
)

popped := make([]string, 0)
for i := 0; i < 5; i++ {
for range 5 {
prb, err := repo.PopAny(ctx)
require.NoError(t, err)
popped = append(popped, prb.Addr.GetDottedIP())
Expand Down
2 changes: 1 addition & 1 deletion internal/testutils/browsing.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func UnpackServerList(resp []byte) []map[string]string {
fieldCount := int(resp[6])
fields := make([]string, 0, fieldCount)
unparsed := resp[8:]
for i := 0; i < fieldCount; i++ {
for range fieldCount {
field, rem := binutils.ConsumeCString(unparsed)
// consume extra null byte at the end of the field
unparsed = rem[1:]
Expand Down
3 changes: 1 addition & 2 deletions internal/testutils/fixtures.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import (
)

func GenRandomIP() net.IP {
var i int
for i = 0; i < 10; i++ {
for i := range 10 {
rand32int := make([]byte, 4)
binary.BigEndian.PutUint32(rand32int, rand.Uint32()) // nolint: gosec
randIP := net.IPv4(rand32int[0], rand32int[1], rand32int[2], rand32int[3])
Expand Down
2 changes: 1 addition & 1 deletion pkg/gamespy/browsing/browsing.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func NewRequest(data []byte) (Request, error) {
func (req *Request) parse() error {
// consume 2 equal strings with game identifier (such as swat4 or swat4xp1)
// because they don't serve any purpose for us, just bin them
for i := 0; i < 2; i++ {
for range 2 {
_, rem := binutils.ConsumeCString(req.unparsed)
if rem == nil {
return ErrInvalidRequestFormat
Expand Down
2 changes: 1 addition & 1 deletion pkg/gamespy/browsing/query/filter/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ func getStructField(v any, fieldName string) (any, error) {
sv := rv.Elem()
st := reflect.TypeOf(sv.Interface())

for i := 0; i < st.NumField(); i++ {
for i := range st.NumField() {
field := st.Field(i)
// ignore private fields
if !field.IsExported() {
Expand Down
5 changes: 2 additions & 3 deletions pkg/gamespy/crypt/crypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func Encrypt(gameSecret [GMSL]byte, challenge [CCHL]byte, data []byte) []byte {
payload := make([]byte, HDRL+len(data))
// init crypt header and fill it with random
// bytes 9-23 will be the crypt key xor'ed with the game secret and the client's challenge
for i := 0; i < HDRL; i++ {
for i := range HDRL {
payload[i] = uint8(random.RandInt(1, 255)) ^ gameSecret[i%GMSL] ^ challenge[i%CCHL]
}
svrChallenge := payload[9:HDRL]
Expand All @@ -75,14 +75,13 @@ func Encrypt(gameSecret [GMSL]byte, challenge [CCHL]byte, data []byte) []byte {
}

func Decrypt(gameSecret [GMSL]byte, clientChallenge [CCHL]byte, data []byte) []byte {
var i uint8
var cryptKey [CRTL]byte
// combine secret key, client and server challenges into a crypt key
svrChOffset := (data[0] ^ 0xec) + 2 // 9
svrChLen := data[svrChOffset-1] ^ 0xea // 14
svrChallenge := data[svrChOffset : svrChOffset+svrChLen] // [9..23)
copy(cryptKey[:], clientChallenge[:])
for i = 0; i < svrChLen; i++ {
for i := range svrChLen {
k := (i * gameSecret[i%GMSL]) % CCHL
cryptKey[k] ^= (cryptKey[i%CCHL] ^ svrChallenge[i]) & 0xFF
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/gamespy/crypt/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func newCipherState(cryptKey [CRTL]byte) cipherState {
var toswap, rsum, keypos uint8
cs := cipherState{}
// Start with state->cards all in order, one of each.
for i := 0; i < 256; i++ {
for i := range 256 {
cs.cards[i] = uint8(i)
}
keypos = 0 // Start with first byte of the crypt key
Expand Down
2 changes: 1 addition & 1 deletion pkg/gamespy/serverquery/params/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func Marshal(v any) (map[string]string, error) {
func marshal(sv reflect.Value) (map[string]string, error) {
params := make(map[string]string)
st := reflect.TypeOf(sv.Interface())
for i := 0; i < st.NumField(); i++ {
for i := range st.NumField() {
field := st.Field(i)
// ignore private fields
if !field.IsExported() {
Expand Down
2 changes: 1 addition & 1 deletion pkg/gamespy/serverquery/params/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func Unmarshal(params map[string]string, v any) error {

func unmarshal(params map[string]string, sv reflect.Value) error {
st := reflect.TypeOf(sv.Interface())
for i := 0; i < st.NumField(); i++ {
for i := range st.NumField() {
field := st.Field(i)
// ignore private fields
if !field.IsExported() {
Expand Down
2 changes: 1 addition & 1 deletion tests/modules/browser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ func TestBrowser_ParseResponse(t *testing.T) {
assert.Equal(t, 9, fieldCount)
fields := make([]string, 0, fieldCount)
unparsed := resp[8:]
for i := 0; i < fieldCount; i++ {
for range fieldCount {
field, rem := binutils.ConsumeCString(unparsed)
assert.True(t, len(field) > 0)
assert.Equal(t, uint8(0), rem[0])
Expand Down
2 changes: 1 addition & 1 deletion tests/modules/exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func TestExporter_MasterMetrics(t *testing.T) {
sendUDP("127.0.0.1:33811", []byte{0x09})

// invalid keepalive request (no prior heartbeat)
for i := 0; i < 2; i++ {
for range 2 {
sendUDP("127.0.0.1:33811", []byte{0x08, 0xde, 0xad, 0xbe, 0xef})
}

Expand Down

0 comments on commit 36d7a5b

Please sign in to comment.