Forráskód Böngészése

ci: Add additional linting via govet (#4557)

- Adds nilness lint to govet
- Configures golangci to clearly show what checks are disabled for govet
- Fixes existing lint violations
- Rename error variables to prevent shadowing issues
John Saigle 3 hete
szülő
commit
702a7343a6
4 módosított fájl, 31 hozzáadás és 15 törlés
  1. 14 0
      .golangci.yml
  2. 11 10
      node/cmd/ccq/http.go
  3. 1 0
      node/pkg/common/scissors_test.go
  4. 5 5
      node/pkg/query/query.go

+ 14 - 0
.golangci.yml

@@ -141,6 +141,20 @@ linters:
         - sloppyLen
         - underef
         - unslice
+    govet:
+      # The following list includes all the linters that are disabled by default.
+      # Uncomment any of these to enable them:
+      enable:
+        # - atomicalign          # Check for non-64-bits-aligned arguments to sync/atomic functions
+        # - deepequalerrors      # Check for calls of reflect.DeepEqual on error values
+        # - fieldalignment       # Find structs that would use less memory if their fields were sorted
+        # - findcall             # Find calls to a particular function
+        - nilness              # Check for redundant or impossible nil comparisons
+        # - reflectvaluecompare  # Check for comparing reflect.Value values with == or reflect.DeepEqual
+        # - shadow               # Check for possible unintended shadowing of variables
+        # - sortslice            # Check the argument type of sort.Slice
+        # - timeformat           # Check for calls of (time.Time).Format or time.Parse with 2006-02-01
+        # - unusedwrite          # Check for unused writes
     nolintlint:
       # Disable to ensure that all nolint directives actually have an effect.
       # Default: false

+ 11 - 10
node/cmd/ccq/http.go

@@ -190,10 +190,10 @@ outer:
 		failedQueriesByUser.WithLabelValues(permEntry.userName).Inc()
 	case res := <-pendingResponse.ch:
 		s.logger.Info("publishing response to client", zap.String("userId", permEntry.userName), zap.String("requestId", requestId))
-		resBytes, err := res.Response.Marshal()
-		if err != nil {
-			s.logger.Error("failed to marshal response", zap.String("userId", permEntry.userName), zap.String("requestId", requestId), zap.Error(err))
-			http.Error(w, err.Error(), http.StatusInternalServerError)
+		resBytes, respMarshalErr := res.Response.Marshal()
+		if respMarshalErr != nil {
+			s.logger.Error("failed to marshal response", zap.String("userId", permEntry.userName), zap.String("requestId", requestId), zap.Error(respMarshalErr))
+			http.Error(w, respMarshalErr.Error(), http.StatusInternalServerError)
 			invalidQueryRequestReceived.WithLabelValues("failed_to_marshal_response").Inc()
 			failedQueriesByUser.WithLabelValues(permEntry.userName).Inc()
 			break
@@ -205,8 +205,9 @@ outer:
 		signatures := make([]string, 0, len(res.Signatures))
 		for _, sig := range res.Signatures {
 			if sig.Index > math.MaxUint8 {
-				s.logger.Error("Signature index out of bounds", zap.Int("sig.Index", sig.Index))
-				http.Error(w, err.Error(), http.StatusInternalServerError)
+				boundsErr := "Signature index out of bounds"
+				s.logger.Error(boundsErr, zap.Int("sig.Index", sig.Index))
+				http.Error(w, boundsErr, http.StatusInternalServerError)
 				invalidQueryRequestReceived.WithLabelValues("failed_to_marshal_response").Inc()
 				failedQueriesByUser.WithLabelValues(permEntry.userName).Inc()
 				break outer
@@ -216,13 +217,13 @@ outer:
 			signatures = append(signatures, signature)
 		}
 		w.Header().Add("Content-Type", "application/json")
-		err = json.NewEncoder(w).Encode(&queryResponse{
+		encodeErr := json.NewEncoder(w).Encode(&queryResponse{
 			Signatures: signatures,
 			Bytes:      hex.EncodeToString(resBytes),
 		})
-		if err != nil {
-			s.logger.Error("failed to encode response", zap.String("userId", permEntry.userName), zap.String("requestId", requestId), zap.Error(err))
-			http.Error(w, err.Error(), http.StatusInternalServerError)
+		if encodeErr != nil {
+			s.logger.Error("failed to encode response", zap.String("userId", permEntry.userName), zap.String("requestId", requestId), zap.Error(encodeErr))
+			http.Error(w, encodeErr.Error(), http.StatusInternalServerError)
 			invalidQueryRequestReceived.WithLabelValues("failed_to_encode_response").Inc()
 			failedQueriesByUser.WithLabelValues(permEntry.userName).Inc()
 			break

+ 1 - 0
node/pkg/common/scissors_test.go

@@ -24,6 +24,7 @@ func getCounterValue(metric *prometheus.CounterVec, runnableName string) float64
 
 func throwNil(ctx context.Context) error {
 	var x *int = nil
+	//nolint:govet // This test function is specifically checking for nil errors, so being flagged for `nilness` is expected
 	*x = 5
 	return nil
 }

+ 5 - 5
node/pkg/query/query.go

@@ -367,15 +367,15 @@ func handleQueryRequestsImpl(
 
 				// Build the list of per chain response publications and the overall query response publication.
 				responses := []*PerChainQueryResponse{}
-				for _, resp := range pq.responses {
-					if resp == nil {
-						qLogger.Error("unexpected null response in pending query!", zap.String("requestID", resp.RequestID), zap.Int("requestIdx", resp.RequestIdx))
+				for _, pqResp := range pq.responses {
+					if pqResp == nil {
+						qLogger.Error("unexpected nil response in pending query!", zap.String("requestID", resp.RequestID))
 						continue
 					}
 
 					responses = append(responses, &PerChainQueryResponse{
-						ChainId:  resp.ChainId,
-						Response: resp.Response,
+						ChainId:  pqResp.ChainId,
+						Response: pqResp.Response,
 					})
 				}