Skip to content

Commit

Permalink
all: resolve unused value and yoda conditionals staticcheck warnings
Browse files Browse the repository at this point in the history
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í <mvdan@mvdan.cc>
Change-Id: Icb5ef7c57fd26c6e2f82bf7309ebcbea1c5d0fbe
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1195056
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
Reviewed-by: Roger Peppe <rogpeppe@gmail.com>
  • Loading branch information
mvdan committed May 23, 2024
1 parent 2adcaa6 commit fca1bcc
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 15 deletions.
8 changes: 4 additions & 4 deletions cue/build/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion cue/format/simplify.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
Expand Down
1 change: 0 additions & 1 deletion cue/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
1 change: 1 addition & 0 deletions internal/httplog/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))

Expand Down
2 changes: 2 additions & 0 deletions internal/vcs/vcs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
18 changes: 9 additions & 9 deletions pkg/list/math.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

Expand All @@ -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
}
}
Expand All @@ -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
}
}
Expand Down Expand Up @@ -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
}

Expand Down

0 comments on commit fca1bcc

Please sign in to comment.