From 50c49ec8f91997970e1b22b605f94153d51fe622 Mon Sep 17 00:00:00 2001 From: Matthieu MOREL Date: Tue, 7 Jan 2025 15:56:38 +0100 Subject: [PATCH] chore: enable unnecessary-stmt from revive (#21398) Signed-off-by: Matthieu MOREL --- .golangci.yaml | 3 +++ .../scm_provider/bitbucket_server_test.go | 21 +++++++------------ cmd/argocd/commands/admin/backup.go | 3 +-- controller/cache/cache.go | 3 +-- controller/cache/info.go | 3 +-- .../generators/application_generator.go | 6 ++---- pkg/apis/application/v1alpha1/types.go | 3 +-- server/application/application.go | 3 +-- util/grpc/errors.go | 3 +-- util/kube/kube.go | 3 +-- util/kustomize/kustomize_test.go | 3 +-- util/lua/custom_actions_test.go | 3 +-- util/lua/lua.go | 3 +-- util/rbac/rbac.go | 18 +++++++--------- 14 files changed, 30 insertions(+), 48 deletions(-) diff --git a/.golangci.yaml b/.golangci.yaml index 3a51e14d3ebe0..acd1813a353cf 100644 --- a/.golangci.yaml +++ b/.golangci.yaml @@ -143,6 +143,9 @@ linters-settings: # warns when an exported function or method returns a value of an un-exported type. - name: unexported-return disabled: true + # https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md#unnecessary-stmt + - name: unnecessary-stmt + disabled: false # spots and proposes to remove unreachable code. also helps to spot errors - name: unreachable-code disabled: false diff --git a/applicationset/services/scm_provider/bitbucket_server_test.go b/applicationset/services/scm_provider/bitbucket_server_test.go index 5b4a957fea1ab..211d86cafd4f8 100644 --- a/applicationset/services/scm_provider/bitbucket_server_test.go +++ b/applicationset/services/scm_provider/bitbucket_server_test.go @@ -307,8 +307,7 @@ func TestGetBranchesBranchPagination(t *testing.T) { func TestGetBranchesDefaultOnly(t *testing.T) { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { assert.Empty(t, r.Header.Get("Authorization")) - switch r.RequestURI { - case "/rest/api/1.0/projects/PROJECT/repos/REPO/branches/default": + if r.RequestURI == "/rest/api/1.0/projects/PROJECT/repos/REPO/branches/default" { _, err := io.WriteString(w, `{ "id": "refs/heads/default", "displayId": "default", @@ -350,8 +349,7 @@ func TestGetBranchesDefaultOnly(t *testing.T) { func TestGetBranchesMissingDefault(t *testing.T) { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { assert.Empty(t, r.Header.Get("Authorization")) - switch r.RequestURI { - case "/rest/api/1.0/projects/PROJECT/repos/REPO/branches/default": + if r.RequestURI == "/rest/api/1.0/projects/PROJECT/repos/REPO/branches/default" { http.Error(w, "Not found", http.StatusNotFound) } defaultHandler(t)(w, r) @@ -373,8 +371,7 @@ func TestGetBranchesMissingDefault(t *testing.T) { func TestGetBranchesEmptyRepo(t *testing.T) { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { assert.Empty(t, r.Header.Get("Authorization")) - switch r.RequestURI { - case "/rest/api/1.0/projects/PROJECT/repos/REPO/branches/default": + if r.RequestURI == "/rest/api/1.0/projects/PROJECT/repos/REPO/branches/default" { return } })) @@ -395,8 +392,7 @@ func TestGetBranchesEmptyRepo(t *testing.T) { func TestGetBranchesErrorDefaultBranch(t *testing.T) { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { assert.Empty(t, r.Header.Get("Authorization")) - switch r.RequestURI { - case "/rest/api/1.0/projects/PROJECT/repos/REPO/branches/default": + if r.RequestURI == "/rest/api/1.0/projects/PROJECT/repos/REPO/branches/default" { http.Error(w, "Internal server error", http.StatusInternalServerError) } defaultHandler(t)(w, r) @@ -510,8 +506,7 @@ func TestListReposBearerAuth(t *testing.T) { func TestListReposDefaultBranch(t *testing.T) { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { assert.Empty(t, r.Header.Get("Authorization")) - switch r.RequestURI { - case "/rest/api/1.0/projects/PROJECT/repos/REPO/branches/default": + if r.RequestURI == "/rest/api/1.0/projects/PROJECT/repos/REPO/branches/default" { _, err := io.WriteString(w, `{ "id": "refs/heads/default", "displayId": "default", @@ -547,8 +542,7 @@ func TestListReposDefaultBranch(t *testing.T) { func TestListReposMissingDefaultBranch(t *testing.T) { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { assert.Empty(t, r.Header.Get("Authorization")) - switch r.RequestURI { - case "/rest/api/1.0/projects/PROJECT/repos/REPO/branches/default": + if r.RequestURI == "/rest/api/1.0/projects/PROJECT/repos/REPO/branches/default" { http.Error(w, "Not found", http.StatusNotFound) } defaultHandler(t)(w, r) @@ -564,8 +558,7 @@ func TestListReposMissingDefaultBranch(t *testing.T) { func TestListReposErrorDefaultBranch(t *testing.T) { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { assert.Empty(t, r.Header.Get("Authorization")) - switch r.RequestURI { - case "/rest/api/1.0/projects/PROJECT/repos/REPO/branches/default": + if r.RequestURI == "/rest/api/1.0/projects/PROJECT/repos/REPO/branches/default" { http.Error(w, "Internal server error", http.StatusInternalServerError) } defaultHandler(t)(w, r) diff --git a/cmd/argocd/commands/admin/backup.go b/cmd/argocd/commands/admin/backup.go index 7b58cd49fae8d..7725ed1d9e5ae 100644 --- a/cmd/argocd/commands/admin/backup.go +++ b/cmd/argocd/commands/admin/backup.go @@ -383,8 +383,7 @@ func checkAppHasNoNeedToStopOperation(liveObj unstructured.Unstructured, stopOpe if !stopOperation { return true } - switch liveObj.GetKind() { - case application.ApplicationKind: + if liveObj.GetKind() == application.ApplicationKind { return liveObj.Object["operation"] == nil } return true diff --git a/controller/cache/cache.go b/controller/cache/cache.go index b4c9819184e88..b649b02d3c1ce 100644 --- a/controller/cache/cache.go +++ b/controller/cache/cache.go @@ -438,8 +438,7 @@ func isResourceQuotaConflictErr(err error) bool { func isTransientNetworkErr(err error) bool { var netErr net.Error - switch { - case errors.As(err, &netErr): + if errors.As(err, &netErr) { var dnsErr *net.DNSError var opErr *net.OpError var unknownNetworkErr net.UnknownNetworkError diff --git a/controller/cache/info.go b/controller/cache/info.go index 8dfa6e1c51bd5..de531d9a9777f 100644 --- a/controller/cache/info.go +++ b/controller/cache/info.go @@ -58,8 +58,7 @@ func populateNodeInfo(un *unstructured.Unstructured, res *ResourceInfo, customLa populateHostNodeInfo(un, res) } case "extensions", "networking.k8s.io": - switch gvk.Kind { - case kube.IngressKind: + if gvk.Kind == kube.IngressKind { populateIngressInfo(un, res) } case "networking.istio.io": diff --git a/hack/gen-resources/generators/application_generator.go b/hack/gen-resources/generators/application_generator.go index 07fb900327426..9a1437d9dd6a8 100644 --- a/hack/gen-resources/generators/application_generator.go +++ b/hack/gen-resources/generators/application_generator.go @@ -40,8 +40,7 @@ func (generator *ApplicationGenerator) buildRandomSource(repositories []*v1alpha } func (generator *ApplicationGenerator) buildSource(opts *util.GenerateOpts, repositories []*v1alpha1.Repository) (*v1alpha1.ApplicationSource, error) { - switch opts.ApplicationOpts.SourceOpts.Strategy { - case "Random": + if opts.ApplicationOpts.SourceOpts.Strategy == "Random" { return generator.buildRandomSource(repositories) } return generator.buildRandomSource(repositories) @@ -57,8 +56,7 @@ func (generator *ApplicationGenerator) buildRandomDestination(opts *util.Generat } func (generator *ApplicationGenerator) buildDestination(opts *util.GenerateOpts, clusters []v1alpha1.Cluster) (*v1alpha1.ApplicationDestination, error) { - switch opts.ApplicationOpts.DestinationOpts.Strategy { - case "Random": + if opts.ApplicationOpts.DestinationOpts.Strategy == "Random" { return generator.buildRandomDestination(opts, clusters) } return generator.buildRandomDestination(opts, clusters) diff --git a/pkg/apis/application/v1alpha1/types.go b/pkg/apis/application/v1alpha1/types.go index d62948721700f..fe2a17f920b0c 100644 --- a/pkg/apis/application/v1alpha1/types.go +++ b/pkg/apis/application/v1alpha1/types.go @@ -288,9 +288,8 @@ func (spec *ApplicationSpec) GetSourcePtrByIndex(sourceIndex int) *ApplicationSo // AllowsConcurrentProcessing returns true if given application source can be processed concurrently func (source *ApplicationSource) AllowsConcurrentProcessing() bool { - switch { // Kustomize with parameters requires changing kustomization.yaml file - case source.Kustomize != nil: + if source.Kustomize != nil { return source.Kustomize.AllowsConcurrentProcessing() } return true diff --git a/server/application/application.go b/server/application/application.go index b260c4ca1f6b2..59d8a755e4d18 100644 --- a/server/application/application.go +++ b/server/application/application.go @@ -2477,8 +2477,7 @@ func (s *Server) RunResourceAction(ctx context.Context, q *application.ResourceA if err != nil { return nil, err } - switch impactedResource.K8SOperation { - case lua.CreateOperation: + if impactedResource.K8SOperation == lua.CreateOperation { createOptions := metav1.CreateOptions{DryRun: []string{"All"}} _, err := s.kubectl.CreateResource(ctx, config, newObj.GroupVersionKind(), newObj.GetName(), newObj.GetNamespace(), newObj, createOptions) if err != nil { diff --git a/util/grpc/errors.go b/util/grpc/errors.go index 755e9bae89d1a..4af523852135a 100644 --- a/util/grpc/errors.go +++ b/util/grpc/errors.go @@ -24,8 +24,7 @@ func gitErrToGRPC(err error) error { errMsg = grpcStatus.Message() } - switch errMsg { - case giterr.ErrRepositoryNotFound.Error(): + if errMsg == giterr.ErrRepositoryNotFound.Error() { err = rewrapError(errors.New(errMsg), codes.NotFound) } return err diff --git a/util/kube/kube.go b/util/kube/kube.go index 6cd1378a2c71f..f6362a78dfc56 100644 --- a/util/kube/kube.go +++ b/util/kube/kube.go @@ -78,8 +78,7 @@ func SetAppInstanceLabel(target *unstructured.Unstructured, key, val string) err } } case "batch": - switch gvk.Kind { - case kube.JobKind: + if gvk.Kind == kube.JobKind { templateLabels, ok, err := unstructured.NestedMap(target.UnstructuredContent(), "spec", "template", "metadata", "labels") if err != nil { return err diff --git a/util/kustomize/kustomize_test.go b/util/kustomize/kustomize_test.go index eb0395fd0c7a9..01ea8d9b963be 100644 --- a/util/kustomize/kustomize_test.go +++ b/util/kustomize/kustomize_test.go @@ -119,8 +119,7 @@ func TestKustomizeBuild(t *testing.T) { } for _, image := range images { - switch image { - case "nginx": + if image == "nginx" { assert.Equal(t, "1.15.5", image) } } diff --git a/util/lua/custom_actions_test.go b/util/lua/custom_actions_test.go index aee5c59a9ac0d..dc8662bc5e294 100644 --- a/util/lua/custom_actions_test.go +++ b/util/lua/custom_actions_test.go @@ -26,8 +26,7 @@ func (t testNormalizer) Normalize(un *unstructured.Unstructured) error { if un == nil { return nil } - switch un.GetKind() { - case "Job": + if un.GetKind() == "Job" { err := unstructured.SetNestedField(un.Object, map[string]any{"name": "not sure why this works"}, "metadata") if err != nil { return fmt.Errorf("failed to normalize Job: %w", err) diff --git a/util/lua/lua.go b/util/lua/lua.go index 6f3501e31d7e0..c51ee9d46a797 100644 --- a/util/lua/lua.go +++ b/util/lua/lua.go @@ -356,8 +356,7 @@ func isActionDisabled(actionsMap any) bool { return false } for key, val := range actions { - switch vv := val.(type) { - case bool: + if vv, ok := val.(bool); ok { if key == "disabled" { return vv } diff --git a/util/rbac/rbac.go b/util/rbac/rbac.go index f977e092074cf..77a8a4cbbdca6 100644 --- a/util/rbac/rbac.go +++ b/util/rbac/rbac.go @@ -249,17 +249,15 @@ func (e *Enforcer) EnforceErr(rvals ...any) error { for i, rval := range rvals[1:] { rvalsStrs[i] = fmt.Sprintf("%s", rval) } - switch s := rvals[0].(type) { - case jwt.Claims: + if s, ok := rvals[0].(jwt.Claims); ok { claims, err := jwtutil.MapClaims(s) - if err != nil { - break - } - if sub := jwtutil.StringField(claims, "sub"); sub != "" { - rvalsStrs = append(rvalsStrs, "sub: "+sub) - } - if issuedAtTime, err := jwtutil.IssuedAtTime(claims); err == nil { - rvalsStrs = append(rvalsStrs, "iat: "+issuedAtTime.Format(time.RFC3339)) + if err == nil { + if sub := jwtutil.StringField(claims, "sub"); sub != "" { + rvalsStrs = append(rvalsStrs, "sub: "+sub) + } + if issuedAtTime, err := jwtutil.IssuedAtTime(claims); err == nil { + rvalsStrs = append(rvalsStrs, "iat: "+issuedAtTime.Format(time.RFC3339)) + } } } errMsg = fmt.Sprintf("%s: %s", errMsg, strings.Join(rvalsStrs, ", "))