Skip to content

Commit 7e8a2c0

Browse files
authored
Apply Go modernize fixes (#489)
Run the modernize analyzer to update code to use newer Go idioms: - Use integer range loops (for i := range N) instead of C-style loops - Use `any` type alias instead of `interface{}` - Remove unnecessary loop variable captures (Go 1.22+) - Use `slices.Contains()` instead of manual search loops - Use `max()` builtin instead of manual clamping - Fix comment whitespace
1 parent 01c0d97 commit 7e8a2c0

File tree

13 files changed

+24
-33
lines changed

13 files changed

+24
-33
lines changed

cache-redis_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ func TestEncodeDecodePooling(t *testing.T) {
150150
}
151151

152152
// Encode multiple times to test pool reuse
153-
for i := 0; i < 100; i++ {
153+
for i := range 100 {
154154
encoded, err := encodeCacheAnswer(item)
155155
if err != nil {
156156
t.Fatalf("iteration %d: encodeCacheAnswer failed: %v", i, err)
@@ -244,7 +244,7 @@ func TestEncodeConcurrent(t *testing.T) {
244244
var wg sync.WaitGroup
245245
wg.Add(numGoroutines)
246246

247-
for g := 0; g < numGoroutines; g++ {
247+
for g := range numGoroutines {
248248
go func(gid int) {
249249
defer wg.Done()
250250

@@ -264,7 +264,7 @@ func TestEncodeConcurrent(t *testing.T) {
264264
Msg: msg,
265265
}
266266

267-
for i := 0; i < numIterations; i++ {
267+
for i := range numIterations {
268268
encoded, err := encodeCacheAnswer(item)
269269
if err != nil {
270270
errs <- err

cmd/routedns/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ arguments.
6565

6666
type Node struct {
6767
id string
68-
value interface{}
68+
value any
6969
}
7070

7171
var _ dag.IDInterface = Node{}

dohclient.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ func (d *DoHClient) do(req *http.Request) (*http.Response, error) {
184184

185185
func (d *DoHClient) buildPostRequest(ctx context.Context, msg []byte) (*http.Request, error) {
186186
// The URL could be a template. Process it without values since POST doesn't use variables in the URL.
187-
u, err := d.template.Expand(map[string]interface{}{})
187+
u, err := d.template.Expand(map[string]any{})
188188
if err != nil {
189189
d.metrics.err.Add("template", 1)
190190
return nil, err
@@ -205,7 +205,7 @@ func (d *DoHClient) buildGetRequest(ctx context.Context, msg []byte) (*http.Requ
205205
b64 := base64.RawURLEncoding.EncodeToString(msg)
206206

207207
// The URL must be a template. Process it with the "dns" param containing the encoded query.
208-
u, err := d.template.Expand(map[string]interface{}{"dns": b64})
208+
u, err := d.template.Expand(map[string]any{"dns": b64})
209209
if err != nil {
210210
d.metrics.err.Add("template", 1)
211211
return nil, err

fastest-tcp.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ func (r *FastestTCP) probeFastest(log *slog.Logger, rrs []dns.RR) ([]dns.RR, err
136136
// Re-order the list in-place to put the fastest at the top
137137
rr := res.rr
138138
err := res.err
139-
for i := 0; i < len(rrs); i++ {
139+
for i := range rrs {
140140
if rrs[i] == rr {
141141
return rrs, err
142142
}
@@ -155,7 +155,7 @@ func (r *FastestTCP) probeAll(log *slog.Logger, rrs []dns.RR) ([]dns.RR, error)
155155
defer cancel()
156156
resultCh := r.probe(ctx, log, rrs)
157157
results := make([]dns.RR, 0, len(rrs))
158-
for i := 0; i < len(rrs); i++ {
158+
for range rrs {
159159
select {
160160
case res := <-resultCh:
161161
if res.err != nil {

fastest.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ func (r *Fastest) Resolve(q *dns.Msg, ci ClientInfo) (*dns.Msg, error) {
3737

3838
// Send the query to all resolvers. The responses are collected in a buffered channel
3939
for _, resolver := range r.resolvers {
40-
resolver := resolver
4140
go func() {
4241
a, err := resolver.Resolve(q, ci)
4342
responseCh <- response{resolver, a, err}

ip-blocklist-trie.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func (t *ipBlocklistTrie) add(n *net.IPNet) {
2424
}
2525
prefix, _ := n.Mask.Size()
2626
p := t.root
27-
for i := 0; i < prefix; i++ {
27+
for i := range prefix {
2828
if p.leaf { // stop if we already have a shorter prefix than this
2929
break
3030
}

lru-cache.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ func (c *lruCache) resize() {
146146
return
147147
}
148148
drop := len(c.items) - c.maxItems
149-
for i := 0; i < drop; i++ {
149+
for range drop {
150150
item := c.tail.prev
151151
item.prev.next = c.tail
152152
c.tail.prev = item.prev

lru-cache_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func TestLRUAddGet(t *testing.T) {
1818
}
1919
var items []item
2020

21-
for i := 0; i < 10; i++ {
21+
for i := range 10 {
2222
msg := new(dns.Msg)
2323
msg.SetQuestion(fmt.Sprintf("test%d.com.", i), dns.TypeA)
2424
msg.Answer = []dns.RR{

mac-db.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ func parseMAC(addr string) ([]byte, error) {
105105
return nil, fmt.Errorf("unable to parse mac address %q, expected format 01:23:45:ab:cd:ef", addr)
106106
}
107107
// Check the format, we need 6 parts with 5 separator characters (:) in it
108-
for i := 0; i < 5; i++ {
108+
for i := range 5 {
109109
if b[(i*3)+2] != ':' {
110110
return nil, fmt.Errorf("unable to parse mac address %q, expected format 01:23:45:ab:cd:ef", addr)
111111
}

padding.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ package rdns
22

33
import "github.com/miekg/dns"
44

5-
// QueryPaddingBlockSize is used to pad queries sent over DoT and DoH according to rfc8467
5+
// QueryPaddingBlockSize is used to pad queries sent over DoT and DoH according to rfc8467
66
const QueryPaddingBlockSize = 128
77

8-
// ResponsePaddingBlockSize is used to pad responses over DoT and DoH according to rfc8467
8+
// ResponsePaddingBlockSize is used to pad responses over DoT and DoH according to rfc8467
99
const ResponsePaddingBlockSize = 468
1010

1111
// Fixed buffers to draw on for padding (rather than allocate every time)
@@ -49,10 +49,9 @@ func padAnswer(q, a *dns.Msg) {
4949
// If padding would make the packet larger than the request EDNS0 allows, we need
5050
// to truncate it.
5151
if len+padLen > int(edns0q.UDPSize()) {
52-
padLen = int(edns0q.UDPSize()) - len
53-
if padLen < 0 { // Still doesn't fit? Give up on padding
54-
padLen = 0
55-
}
52+
padLen = max(int(edns0q.UDPSize())-len,
53+
// Still doesn't fit? Give up on padding
54+
0)
5655
}
5756
paddingOpt.Padding = respPadBuf[0:padLen]
5857
}

0 commit comments

Comments
 (0)