improve code quality (#1228)

* Combine multiple `append`s into a single call

* Fix Yoda conditions

* Fix check for empty string

* revert "combine multiple `append`s"
This commit is contained in:
Shubhendra Singh Chauhan 2021-02-25 21:31:55 +05:30 committed by GitHub
parent 9884b9f446
commit 2f14d104f3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 18 additions and 22 deletions

View File

@ -349,10 +349,7 @@ func ReverseAddr(addr string) (arpa string, err error) {
// Add it, in reverse, to the buffer // Add it, in reverse, to the buffer
for i := len(ip) - 1; i >= 0; i-- { for i := len(ip) - 1; i >= 0; i-- {
v := ip[i] v := ip[i]
buf = append(buf, hexDigit[v&0xF]) buf = append(buf, hexDigit[v&0xF], '.', hexDigit[v>>4], '.')
buf = append(buf, '.')
buf = append(buf, hexDigit[v>>4])
buf = append(buf, '.')
} }
// Append "ip6.arpa." and return (buf already has the final .) // Append "ip6.arpa." and return (buf already has the final .)
buf = append(buf, "ip6.arpa."...) buf = append(buf, "ip6.arpa."...)

View File

@ -325,7 +325,7 @@ Activate: 20110302104537`
} }
switch priv := p.(type) { switch priv := p.(type) {
case *rsa.PrivateKey: case *rsa.PrivateKey:
if 65537 != priv.PublicKey.E { if priv.PublicKey.E != 65537 {
t.Error("exponenent should be 65537") t.Error("exponenent should be 65537")
} }
default: default:

View File

@ -30,10 +30,10 @@ func AddOrigin(s, origin string) string {
if dns.IsFqdn(s) { if dns.IsFqdn(s) {
return s // s is already a FQDN, no need to mess with it. return s // s is already a FQDN, no need to mess with it.
} }
if len(origin) == 0 { if origin == "" {
return s // Nothing to append. return s // Nothing to append.
} }
if s == "@" || len(s) == 0 { if s == "@" || s == "" {
return origin // Expand apex. return origin // Expand apex.
} }
if origin == "." { if origin == "." {
@ -50,7 +50,7 @@ func TrimDomainName(s, origin string) string {
// If the return value ends in a ".", the domain was not the suffix. // If the return value ends in a ".", the domain was not the suffix.
// origin can end in "." or not. Either way the results should be the same. // origin can end in "." or not. Either way the results should be the same.
if len(s) == 0 { if s == "" {
return "@" return "@"
} }
// Someone is using TrimDomainName(s, ".") to remove a dot if it exists. // Someone is using TrimDomainName(s, ".") to remove a dot if it exists.

View File

@ -10,7 +10,7 @@ package dns
// escaped dots (\.) for instance. // escaped dots (\.) for instance.
// s must be a syntactically valid domain name, see IsDomainName. // s must be a syntactically valid domain name, see IsDomainName.
func SplitDomainName(s string) (labels []string) { func SplitDomainName(s string) (labels []string) {
if len(s) == 0 { if s == "" {
return nil return nil
} }
fqdnEnd := 0 // offset of the final '.' or the length of the name fqdnEnd := 0 // offset of the final '.' or the length of the name

View File

@ -245,8 +245,7 @@ func testTXTRRQuick(t *testing.T) {
rrbytes := make([]byte, 0, len(owner)+2+2+4+2+len(rdata)) rrbytes := make([]byte, 0, len(owner)+2+2+4+2+len(rdata))
rrbytes = append(rrbytes, owner...) rrbytes = append(rrbytes, owner...)
rrbytes = append(rrbytes, typeAndClass...) rrbytes = append(rrbytes, typeAndClass...)
rrbytes = append(rrbytes, byte(len(rdata)>>8)) rrbytes = append(rrbytes, byte(len(rdata)>>8), byte(len(rdata)))
rrbytes = append(rrbytes, byte(len(rdata)))
rrbytes = append(rrbytes, rdata...) rrbytes = append(rrbytes, rdata...)
rr, _, err := UnpackRR(rrbytes, 0) rr, _, err := UnpackRR(rrbytes, 0)
if err != nil { if err != nil {

View File

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

View File

@ -662,7 +662,7 @@ East:
Altitude: Altitude:
c.Next() // zBlank c.Next() // zBlank
l, _ = c.Next() l, _ = c.Next()
if len(l.token) == 0 || l.err { if l.token == "" || l.err {
return &ParseError{"", "bad LOC Altitude", l} return &ParseError{"", "bad LOC Altitude", l}
} }
if l.token[len(l.token)-1] == 'M' || l.token[len(l.token)-1] == 'm' { if l.token[len(l.token)-1] == 'M' || l.token[len(l.token)-1] == 'm' {
@ -722,7 +722,7 @@ func (rr *HIP) parse(c *zlexer, o string) *ParseError {
c.Next() // zBlank c.Next() // zBlank
l, _ = c.Next() // zString l, _ = c.Next() // zString
if len(l.token) == 0 || l.err { if l.token == "" || l.err {
return &ParseError{"", "bad HIP Hit", l} return &ParseError{"", "bad HIP Hit", l}
} }
rr.Hit = l.token // This can not contain spaces, see RFC 5205 Section 6. rr.Hit = l.token // This can not contain spaces, see RFC 5205 Section 6.
@ -730,7 +730,7 @@ func (rr *HIP) parse(c *zlexer, o string) *ParseError {
c.Next() // zBlank c.Next() // zBlank
l, _ = c.Next() // zString l, _ = c.Next() // zString
if len(l.token) == 0 || l.err { if l.token == "" || l.err {
return &ParseError{"", "bad HIP PublicKey", l} return &ParseError{"", "bad HIP PublicKey", l}
} }
rr.PublicKey = l.token // This cannot contain spaces rr.PublicKey = l.token // This cannot contain spaces
@ -997,7 +997,7 @@ func (rr *NSEC3) parse(c *zlexer, o string) *ParseError {
rr.Iterations = uint16(i) rr.Iterations = uint16(i)
c.Next() c.Next()
l, _ = c.Next() l, _ = c.Next()
if len(l.token) == 0 || l.err { if l.token == "" || l.err {
return &ParseError{"", "bad NSEC3 Salt", l} return &ParseError{"", "bad NSEC3 Salt", l}
} }
if l.token != "-" { if l.token != "-" {
@ -1007,7 +1007,7 @@ func (rr *NSEC3) parse(c *zlexer, o string) *ParseError {
c.Next() c.Next()
l, _ = c.Next() l, _ = c.Next()
if len(l.token) == 0 || l.err { if l.token == "" || l.err {
return &ParseError{"", "bad NSEC3 NextDomain", l} return &ParseError{"", "bad NSEC3 NextDomain", l}
} }
rr.HashLength = 20 // Fix for NSEC3 (sha1 160 bits) rr.HashLength = 20 // Fix for NSEC3 (sha1 160 bits)

View File

@ -17,7 +17,7 @@ func (rr *SIG) Sign(k crypto.Signer, m *Msg) ([]byte, error) {
if k == nil { if k == nil {
return nil, ErrPrivKey return nil, ErrPrivKey
} }
if rr.KeyTag == 0 || len(rr.SignerName) == 0 || rr.Algorithm == 0 { if rr.KeyTag == 0 || rr.SignerName == "" || rr.Algorithm == 0 {
return nil, ErrKey return nil, ErrKey
} }
@ -78,7 +78,7 @@ func (rr *SIG) Verify(k *KEY, buf []byte) error {
if k == nil { if k == nil {
return ErrKey return ErrKey
} }
if rr.KeyTag == 0 || len(rr.SignerName) == 0 || rr.Algorithm == 0 { if rr.KeyTag == 0 || rr.SignerName == "" || rr.Algorithm == 0 {
return ErrKey return ErrKey
} }

View File

@ -321,7 +321,7 @@ func (s *SVCBAlpn) pack() ([]byte, error) {
// Liberally estimate the size of an alpn as 10 octets // Liberally estimate the size of an alpn as 10 octets
b := make([]byte, 0, 10*len(s.Alpn)) b := make([]byte, 0, 10*len(s.Alpn))
for _, e := range s.Alpn { for _, e := range s.Alpn {
if len(e) == 0 { if e == "" {
return nil, errors.New("dns: svcbalpn: empty alpn-id") return nil, errors.New("dns: svcbalpn: empty alpn-id")
} }
if len(e) > 255 { if len(e) > 255 {
@ -390,7 +390,7 @@ func (*SVCBNoDefaultAlpn) unpack(b []byte) error {
} }
func (*SVCBNoDefaultAlpn) parse(b string) error { func (*SVCBNoDefaultAlpn) parse(b string) error {
if len(b) != 0 { if b != "" {
return errors.New("dns: svcbnodefaultalpn: no_default_alpn must have no value") return errors.New("dns: svcbnodefaultalpn: no_default_alpn must have no value")
} }
return nil return nil

View File

@ -1472,7 +1472,7 @@ func StringToTime(s string) (uint32, error) {
// saltToString converts a NSECX salt to uppercase and returns "-" when it is empty. // saltToString converts a NSECX salt to uppercase and returns "-" when it is empty.
func saltToString(s string) string { func saltToString(s string) string {
if len(s) == 0 { if s == "" {
return "-" return "-"
} }
return strings.ToUpper(s) return strings.ToUpper(s)