Skip to content

Commit 2f14d10

Browse files
authored
improve code quality (miekg#1228)
* Combine multiple `append`s into a single call * Fix Yoda conditions * Fix check for empty string * revert "combine multiple `append`s"
1 parent 9884b9f commit 2f14d10

10 files changed

+18
-22
lines changed

defaults.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -349,10 +349,7 @@ func ReverseAddr(addr string) (arpa string, err error) {
349349
// Add it, in reverse, to the buffer
350350
for i := len(ip) - 1; i >= 0; i-- {
351351
v := ip[i]
352-
buf = append(buf, hexDigit[v&0xF])
353-
buf = append(buf, '.')
354-
buf = append(buf, hexDigit[v>>4])
355-
buf = append(buf, '.')
352+
buf = append(buf, hexDigit[v&0xF], '.', hexDigit[v>>4], '.')
356353
}
357354
// Append "ip6.arpa." and return (buf already has the final .)
358355
buf = append(buf, "ip6.arpa."...)

dnssec_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ Activate: 20110302104537`
325325
}
326326
switch priv := p.(type) {
327327
case *rsa.PrivateKey:
328-
if 65537 != priv.PublicKey.E {
328+
if priv.PublicKey.E != 65537 {
329329
t.Error("exponenent should be 65537")
330330
}
331331
default:

dnsutil/util.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ func AddOrigin(s, origin string) string {
3030
if dns.IsFqdn(s) {
3131
return s // s is already a FQDN, no need to mess with it.
3232
}
33-
if len(origin) == 0 {
33+
if origin == "" {
3434
return s // Nothing to append.
3535
}
36-
if s == "@" || len(s) == 0 {
36+
if s == "@" || s == "" {
3737
return origin // Expand apex.
3838
}
3939
if origin == "." {
@@ -50,7 +50,7 @@ func TrimDomainName(s, origin string) string {
5050
// If the return value ends in a ".", the domain was not the suffix.
5151
// origin can end in "." or not. Either way the results should be the same.
5252

53-
if len(s) == 0 {
53+
if s == "" {
5454
return "@"
5555
}
5656
// Someone is using TrimDomainName(s, ".") to remove a dot if it exists.

labels.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ package dns
1010
// escaped dots (\.) for instance.
1111
// s must be a syntactically valid domain name, see IsDomainName.
1212
func SplitDomainName(s string) (labels []string) {
13-
if len(s) == 0 {
13+
if s == "" {
1414
return nil
1515
}
1616
fqdnEnd := 0 // offset of the final '.' or the length of the name

parse_test.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -245,8 +245,7 @@ func testTXTRRQuick(t *testing.T) {
245245
rrbytes := make([]byte, 0, len(owner)+2+2+4+2+len(rdata))
246246
rrbytes = append(rrbytes, owner...)
247247
rrbytes = append(rrbytes, typeAndClass...)
248-
rrbytes = append(rrbytes, byte(len(rdata)>>8))
249-
rrbytes = append(rrbytes, byte(len(rdata)))
248+
rrbytes = append(rrbytes, byte(len(rdata)>>8), byte(len(rdata)))
250249
rrbytes = append(rrbytes, rdata...)
251250
rr, _, err := UnpackRR(rrbytes, 0)
252251
if err != nil {

scan.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1233,7 +1233,7 @@ func stringToCm(token string) (e, m uint8, ok bool) {
12331233
// 'nn.1' must be treated as 'nn-meters and 10cm, not 1cm.
12341234
cmeters *= 10
12351235
}
1236-
if len(s[0]) == 0 {
1236+
if s[0] == "" {
12371237
// This will allow omitting the 'meter' part, like .01 (meaning 0.01m = 1cm).
12381238
break
12391239
}

scan_rr.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -662,7 +662,7 @@ East:
662662
Altitude:
663663
c.Next() // zBlank
664664
l, _ = c.Next()
665-
if len(l.token) == 0 || l.err {
665+
if l.token == "" || l.err {
666666
return &ParseError{"", "bad LOC Altitude", l}
667667
}
668668
if l.token[len(l.token)-1] == 'M' || l.token[len(l.token)-1] == 'm' {
@@ -722,15 +722,15 @@ func (rr *HIP) parse(c *zlexer, o string) *ParseError {
722722

723723
c.Next() // zBlank
724724
l, _ = c.Next() // zString
725-
if len(l.token) == 0 || l.err {
725+
if l.token == "" || l.err {
726726
return &ParseError{"", "bad HIP Hit", l}
727727
}
728728
rr.Hit = l.token // This can not contain spaces, see RFC 5205 Section 6.
729729
rr.HitLength = uint8(len(rr.Hit)) / 2
730730

731731
c.Next() // zBlank
732732
l, _ = c.Next() // zString
733-
if len(l.token) == 0 || l.err {
733+
if l.token == "" || l.err {
734734
return &ParseError{"", "bad HIP PublicKey", l}
735735
}
736736
rr.PublicKey = l.token // This cannot contain spaces
@@ -997,7 +997,7 @@ func (rr *NSEC3) parse(c *zlexer, o string) *ParseError {
997997
rr.Iterations = uint16(i)
998998
c.Next()
999999
l, _ = c.Next()
1000-
if len(l.token) == 0 || l.err {
1000+
if l.token == "" || l.err {
10011001
return &ParseError{"", "bad NSEC3 Salt", l}
10021002
}
10031003
if l.token != "-" {
@@ -1007,7 +1007,7 @@ func (rr *NSEC3) parse(c *zlexer, o string) *ParseError {
10071007

10081008
c.Next()
10091009
l, _ = c.Next()
1010-
if len(l.token) == 0 || l.err {
1010+
if l.token == "" || l.err {
10111011
return &ParseError{"", "bad NSEC3 NextDomain", l}
10121012
}
10131013
rr.HashLength = 20 // Fix for NSEC3 (sha1 160 bits)

sig0.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func (rr *SIG) Sign(k crypto.Signer, m *Msg) ([]byte, error) {
1717
if k == nil {
1818
return nil, ErrPrivKey
1919
}
20-
if rr.KeyTag == 0 || len(rr.SignerName) == 0 || rr.Algorithm == 0 {
20+
if rr.KeyTag == 0 || rr.SignerName == "" || rr.Algorithm == 0 {
2121
return nil, ErrKey
2222
}
2323

@@ -78,7 +78,7 @@ func (rr *SIG) Verify(k *KEY, buf []byte) error {
7878
if k == nil {
7979
return ErrKey
8080
}
81-
if rr.KeyTag == 0 || len(rr.SignerName) == 0 || rr.Algorithm == 0 {
81+
if rr.KeyTag == 0 || rr.SignerName == "" || rr.Algorithm == 0 {
8282
return ErrKey
8383
}
8484

svcb.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ func (s *SVCBAlpn) pack() ([]byte, error) {
321321
// Liberally estimate the size of an alpn as 10 octets
322322
b := make([]byte, 0, 10*len(s.Alpn))
323323
for _, e := range s.Alpn {
324-
if len(e) == 0 {
324+
if e == "" {
325325
return nil, errors.New("dns: svcbalpn: empty alpn-id")
326326
}
327327
if len(e) > 255 {
@@ -390,7 +390,7 @@ func (*SVCBNoDefaultAlpn) unpack(b []byte) error {
390390
}
391391

392392
func (*SVCBNoDefaultAlpn) parse(b string) error {
393-
if len(b) != 0 {
393+
if b != "" {
394394
return errors.New("dns: svcbnodefaultalpn: no_default_alpn must have no value")
395395
}
396396
return nil

types.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1472,7 +1472,7 @@ func StringToTime(s string) (uint32, error) {
14721472

14731473
// saltToString converts a NSECX salt to uppercase and returns "-" when it is empty.
14741474
func saltToString(s string) string {
1475-
if len(s) == 0 {
1475+
if s == "" {
14761476
return "-"
14771477
}
14781478
return strings.ToUpper(s)

0 commit comments

Comments
 (0)