Skip to content

Commit

Permalink
all: resolve a few more staticcheck warnings
Browse files Browse the repository at this point in the history
Mainly deprecations, but also a few others sprinkled in.
Reduces the number of warnings by 7.

internal/value.ConvertToRuntime is only needed once after this refactor,
and since we're moving away from it, the internal API can go entirely.

Signed-off-by: Daniel Martí <[email protected]>
Change-Id: I775780e27810a644d4679eacfc93eb30ba5dea1d
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1199870
Reviewed-by: Matthew Sackman <[email protected]>
TryBot-Result: CUEcueckoo <[email protected]>
Unity-Result: CUE porcuepine <[email protected]>
  • Loading branch information
mvdan committed Aug 22, 2024
1 parent 68f856f commit 2f19b1a
Show file tree
Hide file tree
Showing 10 changed files with 17 additions and 28 deletions.
6 changes: 3 additions & 3 deletions cmd/cue/cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import (
"cuelang.org/go/internal/core/adt"
"cuelang.org/go/internal/encoding"
"cuelang.org/go/internal/filetypes"
"cuelang.org/go/internal/value"
)

var requestedVersion = os.Getenv("CUE_SYNTAX_OVERRIDE")
Expand Down Expand Up @@ -810,10 +809,11 @@ func buildTools(cmd *Command, args []string) (*cue.Instance, error) {
inst = cue.Merge(insts...)
}

r := value.ConvertToRuntime(inst.Value().Context())
ctx := inst.Value().Context()
for _, b := range binst {
for _, i := range b.Imports {
if _, err := r.Build(i); err != nil {
val := ctx.BuildInstance(i)
if err := val.Err(); err != nil {
return nil, err
}
}
Expand Down
5 changes: 0 additions & 5 deletions cmd/cue/cmd/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,6 @@ func addInjectionFlags(f *pflag.FlagSet, auto, hidden bool) {

type flagName string

type unaddedFlagUse struct {
cmd string
flag flagName
}

// ensureAdded detects if a flag is being used without it first being
// added to the flagSet. Because flagNames are global, it is quite
// easy to accidentally use a flag in a command without adding it to
Expand Down
4 changes: 3 additions & 1 deletion cmd/cue/cmd/get_go.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,9 @@ func typeSignature(params, results []types.Type) *types.Signature {
for i, result := range results {
resultVars[i] = types.NewParam(token.NoPos, nil, "", result)
}
return types.NewSignature(
return types.NewSignatureType(
nil,
nil,
nil,
types.NewTuple(paramVars...),
types.NewTuple(resultVars...),
Expand Down
3 changes: 1 addition & 2 deletions encoding/gocode/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (

"cuelang.org/go/cue"
"cuelang.org/go/cue/errors"
"cuelang.org/go/internal/value"
)

// Config defines options for generation Go code.
Expand Down Expand Up @@ -154,7 +153,7 @@ func Generate(pkgPath string, inst cue.InstanceOrValue, c *Config) (b []byte, er
g.decl(iter.Label(), iter.Value())
}

r := value.ConvertToRuntime(val.Context())
r := (*cue.Runtime)(val.Context())
b, err = r.Marshal(&val)
g.addErr(err)

Expand Down
2 changes: 1 addition & 1 deletion encoding/gocode/gocodec/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func (c *Codec) Encode(v cue.Value, x interface{}) error {
return v.Decode(x)
}

var defaultCodec = New(value.ConvertToRuntime(cuecontext.New()), nil)
var defaultCodec = New(cuecontext.New(), nil)

// Validate calls Validate on a default Codec for the type of x.
func Validate(x interface{}) error {
Expand Down
2 changes: 1 addition & 1 deletion internal/ci/checks/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func checkCommit(dir string) error {
// line because each commit must be signed-off.
lines := strings.Split(body, "\n")
if len(lines) > 1 && lines[1] != "" {
return fmt.Errorf("The second line of a commit message must be blank")
return fmt.Errorf("the second line of a commit message must be blank")
}

// All authors, including co-authors, must have a signed-off trailer by email.
Expand Down
12 changes: 6 additions & 6 deletions internal/envflag/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func Parse[T any](flags *T, env string) error {
v, err := strconv.ParseBool(valueStr)
if err != nil {
// Invalid format, return an error immediately.
return invalidError{
return errInvalid{
fmt.Errorf("invalid bool value for %s: %v", name, err),
}
}
Expand All @@ -85,11 +85,11 @@ func Parse[T any](flags *T, env string) error {
return errors.Join(errs...)
}

// An InvalidError indicates a malformed input string.
var InvalidError = errors.New("invalid value")
// An ErrInvalid indicates a malformed input string.
var ErrInvalid = errors.New("invalid value")

type invalidError struct{ error }
type errInvalid struct{ error }

func (invalidError) Is(err error) bool {
return err == InvalidError
func (errInvalid) Is(err error) bool {
return err == ErrInvalid
}
2 changes: 1 addition & 1 deletion internal/envflag/flag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func invalid[T comparable](want T) func(t *testing.T) {
return func(t *testing.T) {
var x T
err := Init(&x, "TEST_VAR")
qt.Assert(t, qt.ErrorIs(err, InvalidError))
qt.Assert(t, qt.ErrorIs(err, ErrInvalid))
qt.Assert(t, qt.Equals(x, want))
}
}
Expand Down
5 changes: 1 addition & 4 deletions internal/pkg/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,7 @@ func (s *Struct) IsOpen() bool {
}
// The equivalent code for the old implementation.
ot := s.node.OptionalTypes()
if ot&^adt.HasDynamic != 0 {
return true
}
return false
return ot&^adt.HasDynamic != 0
}

// A ValidationError indicates an error that is only valid if a builtin is used
Expand Down
4 changes: 0 additions & 4 deletions internal/value/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ import (
"cuelang.org/go/internal/types"
)

func ConvertToRuntime(c *cue.Context) *cue.Runtime {
return (*cue.Runtime)(c)
}

func ConvertToContext[Ctx *cue.Runtime | *cue.Context](ctx Ctx) *cue.Context {
if ctx, ok := any(ctx).(*cue.Runtime); ok {
(*runtime.Runtime)(ctx).Init()
Expand Down

0 comments on commit 2f19b1a

Please sign in to comment.