From fca1bcc3e1ea57373ad8724854ebbd2de2ee0855 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Mart=C3=AD?= Date: Wed, 22 May 2024 12:18:41 +0100 Subject: [PATCH] all: resolve unused value and yoda conditionals staticcheck warnings MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Values which are assigned to a variable but unused are either benign, which cause the code to be confusing to the reader, or actually lead to bugs, such as missed error checks in some tests. Also, consistently place the constant on the right side in comparisons. Signed-off-by: Daniel Martí Change-Id: Icb5ef7c57fd26c6e2f82bf7309ebcbea1c5d0fbe Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1195056 TryBot-Result: CUEcueckoo Unity-Result: CUE porcuepine Reviewed-by: Roger Peppe --- cue/build/import.go | 8 ++++---- cue/format/simplify.go | 2 +- cue/types.go | 1 - internal/httplog/client_test.go | 1 + internal/vcs/vcs_test.go | 2 ++ pkg/list/math.go | 18 +++++++++--------- 6 files changed, 17 insertions(+), 15 deletions(-) diff --git a/cue/build/import.go b/cue/build/import.go index 996edb0af..99bfbc711 100644 --- a/cue/build/import.go +++ b/cue/build/import.go @@ -89,10 +89,10 @@ func (inst *Instance) complete() errors.Error { if inst.loadFunc != nil { for i, path := range paths { - isLocal := IsLocalImport(path) - if isLocal { - // path = dirToImportPath(filepath.Join(dir, path)) - } + // isLocal := IsLocalImport(path) + // if isLocal { + // path = dirToImportPath(filepath.Join(dir, path)) + // } imp := c.imports[path] if imp == nil { diff --git a/cue/format/simplify.go b/cue/format/simplify.go index ee8948a42..f9a2b0f2b 100644 --- a/cue/format/simplify.go +++ b/cue/format/simplify.go @@ -52,7 +52,7 @@ func (s *labelSimplifier) processDecls(decls []ast.Decl) { for _, d := range decls { switch x := d.(type) { case *ast.Field: - x = astutil.Apply(x, sc.replace, nil).(*ast.Field) + astutil.Apply(x, sc.replace, nil) } } } diff --git a/cue/types.go b/cue/types.go index d658ee024..a50149556 100644 --- a/cue/types.go +++ b/cue/types.go @@ -2368,7 +2368,6 @@ func (v Value) Expr() (Op, []Value) { case 1: // the default case, processed below. - env = c.Env env, expr = c.EnvExpr() if w, ok := expr.(*adt.Vertex); ok { return Value{v.idx, w, v.parent_}.Expr() diff --git a/internal/httplog/client_test.go b/internal/httplog/client_test.go index 13bb584eb..664bb9965 100644 --- a/internal/httplog/client_test.go +++ b/internal/httplog/client_test.go @@ -32,6 +32,7 @@ func TestTransportWithSlog(t *testing.T) { resp, err := client.Get(srv.URL + "/foo/bar?foo=bar") qt.Assert(t, qt.IsNil(err)) data, err := io.ReadAll(resp.Body) + qt.Assert(t, qt.IsNil(err)) resp.Body.Close() qt.Assert(t, qt.Equals(string(data), "hello")) diff --git a/internal/vcs/vcs_test.go b/internal/vcs/vcs_test.go index a493079c6..82a16015e 100644 --- a/internal/vcs/vcs_test.go +++ b/internal/vcs/vcs_test.go @@ -76,11 +76,13 @@ func TestGit(t *testing.T) { qt.Assert(t, qt.IsTrue(!status.CommitTime.Before(commitTime))) qt.Assert(t, qt.Matches(status.Revision, `[0-9a-f]+`)) files, err := v.ListFiles(ctx, filepath.Join(dir, "subdir")) + qt.Assert(t, qt.IsNil(err)) qt.Assert(t, qt.DeepEquals(files, []string{ "bar/baz", "foo", })) files, err = v.ListFiles(ctx, dir) + qt.Assert(t, qt.IsNil(err)) qt.Assert(t, qt.DeepEquals(files, []string{ "bar.txt", "baz/something", diff --git a/pkg/list/math.go b/pkg/list/math.go index 602ebd6a9..17de18a92 100644 --- a/pkg/list/math.go +++ b/pkg/list/math.go @@ -24,7 +24,7 @@ import ( // Avg returns the average value of a non empty list xs. func Avg(xs []*internal.Decimal) (*internal.Decimal, error) { - if 0 == len(xs) { + if len(xs) == 0 { return nil, fmt.Errorf("empty list") } @@ -47,13 +47,13 @@ func Avg(xs []*internal.Decimal) (*internal.Decimal, error) { // Max returns the maximum value of a non empty list xs. func Max(xs []*internal.Decimal) (*internal.Decimal, error) { - if 0 == len(xs) { + if len(xs) == 0 { return nil, fmt.Errorf("empty list") } max := xs[0] for _, x := range xs[1:] { - if -1 == max.Cmp(x) { + if max.Cmp(x) == -1 { max = x } } @@ -62,13 +62,13 @@ func Max(xs []*internal.Decimal) (*internal.Decimal, error) { // Min returns the minimum value of a non empty list xs. func Min(xs []*internal.Decimal) (*internal.Decimal, error) { - if 0 == len(xs) { + if len(xs) == 0 { return nil, fmt.Errorf("empty list") } min := xs[0] for _, x := range xs[1:] { - if +1 == min.Cmp(x) { + if min.Cmp(x) == +1 { min = x } } @@ -102,22 +102,22 @@ func Range(start, limit, step *internal.Decimal) ([]*internal.Decimal, error) { return nil, fmt.Errorf("step must be non zero") } - if !step.Negative && +1 == start.Cmp(limit) { + if !step.Negative && start.Cmp(limit) == +1 { return nil, fmt.Errorf("end must be greater than start when step is positive") } - if step.Negative && -1 == start.Cmp(limit) { + if step.Negative && start.Cmp(limit) == -1 { return nil, fmt.Errorf("end must be less than start when step is negative") } var vals []*internal.Decimal num := start for { - if !step.Negative && -1 != num.Cmp(limit) { + if !step.Negative && num.Cmp(limit) != -1 { break } - if step.Negative && +1 != num.Cmp(limit) { + if step.Negative && num.Cmp(limit) != +1 { break } -- 2.51.2