Add unconvert linter

To find unnecessary type convertions
This commit is contained in:
Daniel Nephin 2020-04-16 13:35:28 -04:00
parent 7e1734b1c6
commit 2e0f750f1a
17 changed files with 21 additions and 22 deletions

View File

@ -3,6 +3,7 @@ linters:
enable: enable:
- gofmt - gofmt
- govet - govet
- unconvert
issues: issues:
# Disable the default exclude list so that all excludes are explicitly # Disable the default exclude list so that all excludes are explicitly

View File

@ -323,7 +323,7 @@ func TestAgent_makeNodeID(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
if _, err := uuid.ParseUUID(string(id)); err != nil { if _, err := uuid.ParseUUID(id); err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }

View File

@ -9,5 +9,6 @@ import "golang.org/x/sys/unix"
func getrlimit() (uint64, error) { func getrlimit() (uint64, error) {
var limit unix.Rlimit var limit unix.Rlimit
err := unix.Getrlimit(unix.RLIMIT_NOFILE, &limit) err := unix.Getrlimit(unix.RLIMIT_NOFILE, &limit)
// nolint:unconvert // Rlimit.Cur may not be uint64 on all platforms
return uint64(limit.Cur), err return uint64(limit.Cur), err
} }

View File

@ -162,7 +162,7 @@ func (v *Validator) ValidateLogin(ctx context.Context, loginToken string) (*auth
var ( var (
saNamespace = parts[2] saNamespace = parts[2]
saName = parts[3] saName = parts[3]
saUID = string(trResp.Status.User.UID) saUID = trResp.Status.User.UID
) )
// Check to see if there is an override name on the ServiceAccount object. // Check to see if there is an override name on the ServiceAccount object.

View File

@ -43,7 +43,7 @@ func (c *Client) RequestAutoEncryptCerts(servers []string, port int, token strin
id := &connect.SpiffeIDAgent{ id := &connect.SpiffeIDAgent{
Host: dummyTrustDomain, Host: dummyTrustDomain,
Datacenter: c.config.Datacenter, Datacenter: c.config.Datacenter,
Agent: string(c.config.NodeName), Agent: c.config.NodeName,
} }
conf, err := c.config.CAConfig.GetCommonConfig() conf, err := c.config.CAConfig.GetCommonConfig()
@ -71,7 +71,7 @@ func (c *Client) RequestAutoEncryptCerts(servers []string, port int, token strin
// //
// The Common Name includes the dummy trust domain for now but Server will // The Common Name includes the dummy trust domain for now but Server will
// override this when it is signed anyway so it's OK. // override this when it is signed anyway so it's OK.
cn := connect.AgentCN(string(c.config.NodeName), dummyTrustDomain) cn := connect.AgentCN(c.config.NodeName, dummyTrustDomain)
csr, err := connect.CreateCSR(id, cn, pk, dnsNames, ipAddresses) csr, err := connect.CreateCSR(id, cn, pk, dnsNames, ipAddresses)
if err != nil { if err != nil {
return errFn(err) return errFn(err)

View File

@ -28,9 +28,6 @@ const (
// maxPeerRetries limits how many invalidate attempts are made // maxPeerRetries limits how many invalidate attempts are made
maxPeerRetries = 6 maxPeerRetries = 6
// peerRetryBase is a baseline retry time
peerRetryBase = 1 * time.Second
) )
// setupSerf is used to setup and initialize a Serf // setupSerf is used to setup and initialize a Serf
@ -360,7 +357,7 @@ func (s *Server) maybeBootstrap() {
for attempt := uint(0); attempt < maxPeerRetries; attempt++ { for attempt := uint(0); attempt < maxPeerRetries; attempt++ {
if err := s.connPool.RPC(s.config.Datacenter, server.ShortName, server.Addr, server.Version, if err := s.connPool.RPC(s.config.Datacenter, server.ShortName, server.Addr, server.Version,
"Status.Peers", &structs.DCSpecificRequest{Datacenter: s.config.Datacenter}, &peers); err != nil { "Status.Peers", &structs.DCSpecificRequest{Datacenter: s.config.Datacenter}, &peers); err != nil {
nextRetry := time.Duration((1 << attempt) * peerRetryBase) nextRetry := (1 << attempt) * time.Second
s.logger.Error("Failed to confirm peer status for server (will retry).", s.logger.Error("Failed to confirm peer status for server (will retry).",
"server", server.Name, "server", server.Name,
"retry_interval", nextRetry.String(), "retry_interval", nextRetry.String(),

View File

@ -392,7 +392,7 @@ func TestStateStore_EnsureRegistration_Restore(t *testing.T) {
Address: "1.2.3.4", Address: "1.2.3.4",
} }
nodeID := string(req.ID) nodeID := string(req.ID)
nodeName := string(req.Node) nodeName := req.Node
restore := s.Restore() restore := s.Restore()
if err := restore.Registration(1, req); err != nil { if err := restore.Registration(1, req); err != nil {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
@ -2106,7 +2106,7 @@ func TestStateStore_ConnectServiceNodes(t *testing.T) {
ws = memdb.NewWatchSet() ws = memdb.NewWatchSet()
idx, nodes, err = s.ConnectServiceNodes(ws, "db", nil) idx, nodes, err = s.ConnectServiceNodes(ws, "db", nil)
assert.Nil(err) assert.Nil(err)
assert.Equal(idx, uint64(idx)) assert.Equal(idx, uint64(17))
assert.Len(nodes, 3) assert.Len(nodes, 3)
for _, n := range nodes { for _, n := range nodes {
@ -3562,7 +3562,7 @@ func TestStateStore_CheckConnectServiceNodes(t *testing.T) {
ws = memdb.NewWatchSet() ws = memdb.NewWatchSet()
idx, nodes, err = s.CheckConnectServiceNodes(ws, "db", nil) idx, nodes, err = s.CheckConnectServiceNodes(ws, "db", nil)
assert.Nil(err) assert.Nil(err)
assert.Equal(idx, uint64(idx)) assert.Equal(idx, uint64(20))
assert.Len(nodes, 2) assert.Len(nodes, 2)
for _, n := range nodes { for _, n := range nodes {

View File

@ -4270,7 +4270,7 @@ func TestDNS_TCP_and_UDP_Truncate(t *testing.T) {
if protocol == "udp" { if protocol == "udp" {
maxSz = 8192 maxSz = 8192
} }
m.SetEdns0(uint16(maxSz), true) m.SetEdns0(maxSz, true)
c := new(dns.Client) c := new(dns.Client)
c.Net = protocol c.Net = protocol
m.Compress = compress m.Compress = compress

View File

@ -972,7 +972,7 @@ func TestParseConsistencyAndMaxStale(t *testing.T) {
ensureConsistency(t, a, "/v1/catalog/nodes?stale&max_stale=3s", 3*time.Second, false) ensureConsistency(t, a, "/v1/catalog/nodes?stale&max_stale=3s", 3*time.Second, false)
// stale by defaul on discovery // stale by defaul on discovery
a.config.DiscoveryMaxStale = time.Duration(7 * time.Second) a.config.DiscoveryMaxStale = 7 * time.Second
ensureConsistency(t, a, "/v1/catalog/nodes", a.config.DiscoveryMaxStale, false) ensureConsistency(t, a, "/v1/catalog/nodes", a.config.DiscoveryMaxStale, false)
// Not in KV // Not in KV
ensureConsistency(t, a, "/v1/kv/my/path", 0, false) ensureConsistency(t, a, "/v1/kv/my/path", 0, false)

View File

@ -449,7 +449,7 @@ func (e *ServiceSplitterConfigEntry) Normalize() error {
func NormalizeServiceSplitWeight(weight float32) float32 { func NormalizeServiceSplitWeight(weight float32) float32 {
weightScaled := scaleWeight(weight) weightScaled := scaleWeight(weight)
return float32(float32(weightScaled) / 100.0) return float32(weightScaled) / 100.0
} }
func (e *ServiceSplitterConfigEntry) Validate() error { func (e *ServiceSplitterConfigEntry) Validate() error {

View File

@ -529,7 +529,7 @@ func ParseDurationFunc() mapstructure.DecodeHookFunc {
func Uint8ToString(bs []uint8) string { func Uint8ToString(bs []uint8) string {
b := make([]byte, len(bs)) b := make([]byte, len(bs))
for i, v := range bs { for i, v := range bs {
b[i] = byte(v) b[i] = v
} }
return string(b) return string(b)
} }

View File

@ -170,7 +170,7 @@ type HealthChecks []*HealthCheck
func (c HealthChecks) AggregatedStatus() string { func (c HealthChecks) AggregatedStatus() string {
var passing, warning, critical, maintenance bool var passing, warning, critical, maintenance bool
for _, check := range c { for _, check := range c {
id := string(check.CheckID) id := check.CheckID
if id == NodeMaint || strings.HasPrefix(id, ServiceMaintPrefix) { if id == NodeMaint || strings.HasPrefix(id, ServiceMaintPrefix) {
maintenance = true maintenance = true
continue continue

View File

@ -60,7 +60,7 @@ func (op *Operator) RaftRemovePeerByAddress(address string, q *WriteOptions) err
r := op.c.newRequest("DELETE", "/v1/operator/raft/peer") r := op.c.newRequest("DELETE", "/v1/operator/raft/peer")
r.setWriteOptions(q) r.setWriteOptions(q)
r.params.Set("address", string(address)) r.params.Set("address", address)
_, resp, err := requireOK(op.c.doRequest(r)) _, resp, err := requireOK(op.c.doRequest(r))
if err != nil { if err != nil {
@ -77,7 +77,7 @@ func (op *Operator) RaftRemovePeerByID(id string, q *WriteOptions) error {
r := op.c.newRequest("DELETE", "/v1/operator/raft/peer") r := op.c.newRequest("DELETE", "/v1/operator/raft/peer")
r.setWriteOptions(q) r.setWriteOptions(q)
r.params.Set("id", string(id)) r.params.Set("id", id)
_, resp, err := requireOK(op.c.doRequest(r)) _, resp, err := requireOK(op.c.doRequest(r))
if err != nil { if err != nil {

View File

@ -65,7 +65,7 @@ func (c *cmd) Run(args []string) int {
return 1 return 1
} }
entry, err := parseConfigEntry(string(data)) entry, err := parseConfigEntry(data)
if err != nil { if err != nil {
c.UI.Error(fmt.Sprintf("Failed to decode config entry input: %v", err)) c.UI.Error(fmt.Sprintf("Failed to decode config entry input: %v", err))
return 1 return 1

View File

@ -109,7 +109,7 @@ func (c *cmd) Run(args []string) int {
} }
for _, k := range keys { for _, k := range keys {
c.UI.Info(string(k)) c.UI.Info(k)
} }
return 0 return 0

View File

@ -92,7 +92,7 @@ func (c *cmd) Run(args []string) int {
c.UI.Output(" Name: " + nodeName) c.UI.Output(" Name: " + nodeName)
c.UI.Output(" Reason: " + check.Notes) c.UI.Output(" Reason: " + check.Notes)
c.UI.Output("") c.UI.Output("")
} else if strings.HasPrefix(string(check.CheckID), "_service_maintenance:") { } else if strings.HasPrefix(check.CheckID, "_service_maintenance:") {
c.UI.Output("Service:") c.UI.Output("Service:")
c.UI.Output(" ID: " + check.ServiceID) c.UI.Output(" ID: " + check.ServiceID)
c.UI.Output(" Reason: " + check.Notes) c.UI.Output(" Reason: " + check.Notes)

View File

@ -213,7 +213,7 @@ func stringifyMetadataValue(rawValue interface{}) (string, bool) {
case int32: case int32:
return strconv.FormatInt(int64(v), 10), true return strconv.FormatInt(int64(v), 10), true
case int64: case int64:
return strconv.FormatInt(int64(v), 10), true return strconv.FormatInt(v, 10), true
case int: case int:
return strconv.FormatInt(int64(v), 10), true return strconv.FormatInt(int64(v), 10), true
case uint8: case uint8: