Fix govet+staticcheck issues (#20263)

This is better: https://github.com/golang/go/issues/60529
This commit is contained in:
Klaus Post
2024-08-14 10:11:51 -07:00
committed by GitHub
parent 51b1f41518
commit 3ffeabdfcb
16 changed files with 49 additions and 37 deletions

View File

@@ -49,19 +49,19 @@ func ParsePublicCertFile(certFile string) (x509Certs []*x509.Certificate, err er
for len(current) > 0 {
var pemBlock *pem.Block
if pemBlock, current = pem.Decode(current); pemBlock == nil {
return nil, ErrTLSUnexpectedData(nil).Msg("Could not read PEM block from file %s", certFile)
return nil, ErrTLSUnexpectedData(nil).Msgf("Could not read PEM block from file %s", certFile)
}
var x509Cert *x509.Certificate
if x509Cert, err = x509.ParseCertificate(pemBlock.Bytes); err != nil {
return nil, ErrTLSUnexpectedData(nil).Msg("Failed to parse `%s`: %s", certFile, err.Error())
return nil, ErrTLSUnexpectedData(nil).Msgf("Failed to parse `%s`: %s", certFile, err.Error())
}
x509Certs = append(x509Certs, x509Cert)
}
if len(x509Certs) == 0 {
return nil, ErrTLSUnexpectedData(nil).Msg("Empty public certificate file %s", certFile)
return nil, ErrTLSUnexpectedData(nil).Msgf("Empty public certificate file %s", certFile)
}
return x509Certs, nil
@@ -73,18 +73,18 @@ func ParsePublicCertFile(certFile string) (x509Certs []*x509.Certificate, err er
func LoadX509KeyPair(certFile, keyFile string) (tls.Certificate, error) {
certPEMBlock, err := os.ReadFile(certFile)
if err != nil {
return tls.Certificate{}, ErrTLSReadError(nil).Msg("Unable to read the public key: %s", err)
return tls.Certificate{}, ErrTLSReadError(nil).Msgf("Unable to read the public key: %s", err)
}
keyPEMBlock, err := os.ReadFile(keyFile)
if err != nil {
return tls.Certificate{}, ErrTLSReadError(nil).Msg("Unable to read the private key: %s", err)
return tls.Certificate{}, ErrTLSReadError(nil).Msgf("Unable to read the private key: %s", err)
}
key, rest := pem.Decode(keyPEMBlock)
if len(rest) > 0 {
return tls.Certificate{}, ErrTLSUnexpectedData(nil).Msg("The private key contains additional data")
return tls.Certificate{}, ErrTLSUnexpectedData(nil).Msgf("The private key contains additional data")
}
if key == nil {
return tls.Certificate{}, ErrTLSUnexpectedData(nil).Msg("The private key is not readable")
return tls.Certificate{}, ErrTLSUnexpectedData(nil).Msgf("The private key is not readable")
}
if x509.IsEncryptedPEMBlock(key) {
password := env.Get(EnvCertPassword, "")

View File

@@ -58,9 +58,20 @@ func (u Err) Error() string {
}
// Msg - Replace the current error's message
func (u Err) Msg(m string, args ...interface{}) Err {
func (u Err) Msg(m string) Err {
e := u.Clone()
e.msg = fmt.Sprintf(m, args...)
e.msg = m
return e
}
// Msgf - Replace the current error's message
func (u Err) Msgf(m string, args ...interface{}) Err {
e := u.Clone()
if len(args) == 0 {
e.msg = m
} else {
e.msg = fmt.Sprintf(m, args...)
}
return e
}

View File

@@ -23,14 +23,10 @@ package disk
import (
"os"
"reflect"
"runtime"
"testing"
)
func TestReadDriveStats(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("skipping this test in windows")
}
testCases := []struct {
stat string
expectedIOStats IOStats

View File

@@ -210,7 +210,7 @@ const (
func init() {
// Static check if we exceed 255 handler ids.
// Extend the type to uint16 when hit.
if handlerLast > 255 {
if uint32(handlerLast) > 255 {
panic(fmt.Sprintf("out of handler IDs. %d > %d", handlerLast, 255))
}
}
@@ -435,6 +435,7 @@ func recycleFunc[RT RoundTripper](newRT func() RT) (newFn func() RT, recycle fun
return func() RT { return pool.Get().(RT) },
func(r RT) {
if r != rZero {
//nolint:staticcheck // SA6002 IT IS A GENERIC VALUE!
pool.Put(r)
}
}
@@ -601,15 +602,18 @@ func GetCaller(ctx context.Context) *RemoteClient {
// GetSubroute returns caller information from contexts provided to handlers.
func GetSubroute(ctx context.Context) string {
//nolint:staticcheck // SA1029 Staticcheck is drunk.
val, _ := ctx.Value(ctxSubrouteKey{}).(string)
return val
}
func setCaller(ctx context.Context, cl *RemoteClient) context.Context {
//nolint:staticcheck // SA1029 Staticcheck is drunk.
return context.WithValue(ctx, ctxCallerKey{}, cl)
}
func setSubroute(ctx context.Context, s string) context.Context {
//nolint:staticcheck // SA1029 Staticcheck is drunk.
return context.WithValue(ctx, ctxSubrouteKey{}, s)
}
@@ -681,6 +685,7 @@ func (h *StreamTypeHandler[Payload, Req, Resp]) NewRequest() Req {
// These should be returned by the handler.
func (h *StreamTypeHandler[Payload, Req, Resp]) PutRequest(r Req) {
if r != h.nilReq {
//nolint:staticcheck // SA6002 IT IS A GENERIC VALUE! (and always a pointer)
h.reqPool.Put(r)
}
}
@@ -689,6 +694,7 @@ func (h *StreamTypeHandler[Payload, Req, Resp]) PutRequest(r Req) {
// These should be returned by the caller.
func (h *StreamTypeHandler[Payload, Req, Resp]) PutResponse(r Resp) {
if r != h.nilResp {
//nolint:staticcheck // SA6002 IT IS A GENERIC VALUE! (and always a pointer)
h.respPool.Put(r)
}
}

View File

@@ -598,6 +598,7 @@ func (p *ArrayOf[T]) newA(sz uint32) []T {
func (p *ArrayOf[T]) putA(v []T) {
var zero T // nil
for i, t := range v {
//nolint:staticcheck // SA6002 IT IS A GENERIC VALUE!
p.ePool.Put(t)
v[i] = zero
}

View File

@@ -18,7 +18,6 @@
package pubsub
import (
"fmt"
"testing"
"time"
)
@@ -138,7 +137,7 @@ func TestPubSub(t *testing.T) {
ps.Publish(val)
msg := <-ch1
if msg != val {
t.Fatalf(fmt.Sprintf("expected %s , found %s", val, msg))
t.Fatalf("expected %s , found %s", val, msg)
}
}
@@ -160,7 +159,7 @@ func TestMultiPubSub(t *testing.T) {
msg1 := <-ch1
msg2 := <-ch2
if msg1 != val && msg2 != val {
t.Fatalf(fmt.Sprintf("expected both subscribers to have%s , found %s and %s", val, msg1, msg2))
t.Fatalf("expected both subscribers to have%s , found %s and %s", val, msg1, msg2)
}
}
@@ -189,12 +188,12 @@ func TestMultiPubSubMask(t *testing.T) {
msg1 := <-ch1
msg2 := <-ch2
if msg1 != val && msg2 != val {
t.Fatalf(fmt.Sprintf("expected both subscribers to have%s , found %s and %s", val, msg1, msg2))
t.Fatalf("expected both subscribers to have%s , found %s and %s", val, msg1, msg2)
}
select {
case msg := <-ch3:
t.Fatalf(fmt.Sprintf("unexpected msg, f got %s", msg))
t.Fatalf("unexpected msg, f got %s", msg)
default:
}
}