Skip to content

Commit

Permalink
cue: resolve a few staticcheck warnings
Browse files Browse the repository at this point in the history
Instance's setError and eval methods are unexported and unused.
Moreover, Instance itself has been deprecated for years.

hasDisjunction and stripNonDefaults, as well as their uses in the
commented-out bit of code in Value.Default,
have been unused for at least four years. They are still present
in the git history if needed, but clearly they are not needed now
and have not actually been for a long time already.

Finally, rewrite the way a test advances an iterator twice
so that it doesn't make staticcheck think that we mistakenly
checked the same condition twice, which is often due to human error.

Updates #3335.

Signed-off-by: Daniel Martí <[email protected]>
Change-Id: If6608b5d9d1076316e3c03e530b6b6577a157605
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1202602
Reviewed-by: Roger Peppe <[email protected]>
TryBot-Result: CUEcueckoo <[email protected]>
Unity-Result: CUE porcuepine <[email protected]>
  • Loading branch information
mvdan committed Oct 18, 2024
1 parent d9fda8c commit 537f744
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 141 deletions.
11 changes: 0 additions & 11 deletions cue/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,17 +165,6 @@ func (inst *Instance) setListOrError(err errors.Error) {
inst.Err = errors.Append(inst.Err, err)
}

func (inst *Instance) setError(err errors.Error) {
inst.Incomplete = true
inst.Err = errors.Append(inst.Err, err)
}

func (inst *Instance) eval(ctx *adt.OpContext) adt.Value {
// TODO: remove manifest here?
v := manifest(ctx, inst.root)
return v
}

// ID returns the package identifier that uniquely qualifies module and
// package name.
func (inst *Instance) ID() string {
Expand Down
127 changes: 0 additions & 127 deletions cue/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -719,133 +719,6 @@ func (v Value) Default() (Value, bool) {
return v, false
}
return makeValue(v.idx, d, v.parent_), true

// d, ok := v.v.Value.(*adt.Disjunction)
// if !ok {
// return v, false
// }

// var w *adt.Vertex

// switch d.NumDefaults {
// case 0:
// return v, false

// case 1:
// w = d.Values[0]

// default:
// x := *v.v
// x.Value = &adt.Disjunction{
// Src: d.Src,
// Values: d.Values[:d.NumDefaults],
// NumDefaults: 0,
// }
// w = &x
// }

// w.Conjuncts = nil
// for _, c := range v.v.Conjuncts {
// // TODO: preserve field information.
// expr, _ := stripNonDefaults(c.Expr())
// w.AddConjunct(adt.MakeConjunct(c.Env, expr))
// }

// return makeValue(v.idx, w), true

// if !stripped {
// return v, false
// }

// n := *v.v
// n.Conjuncts = conjuncts
// return Value{v.idx, &n}, true

// isDefault := false
// for _, c := range v.v.Conjuncts {
// if hasDisjunction(c.Expr()) {
// isDefault = true
// break
// }
// }

// if !isDefault {
// return v, false
// }

// TODO: record expanded disjunctions in output.
// - Rename Disjunction to DisjunctionExpr
// - Introduce Disjuncts with Values.
// - In Expr introduce Star
// - Don't pick default by default?

// Evaluate the value.
// x := eval.FinalizeValue(v.idx.Runtime, v.v)
// if b, _ := x.Value.(*adt.Bottom); b != nil { // && b.IsIncomplete() {
// return v, false
// }
// // Finalize and return here.
// return Value{v.idx, x}, isDefault
}

// TODO: this should go: record preexpanded disjunctions in Vertex.
func hasDisjunction(expr adt.Expr) bool {
switch x := expr.(type) {
case *adt.DisjunctionExpr:
return true
case *adt.Conjunction:
for _, v := range x.Values {
if hasDisjunction(v) {
return true
}
}
case *adt.BinaryExpr:
switch x.Op {
case adt.OrOp:
return true
case adt.AndOp:
return hasDisjunction(x.X) || hasDisjunction(x.Y)
}
}
return false
}

// TODO: this should go: record preexpanded disjunctions in Vertex.
func stripNonDefaults(expr adt.Expr) (r adt.Expr, stripped bool) {
switch x := expr.(type) {
case *adt.DisjunctionExpr:
if !x.HasDefaults {
return x, false
}
d := *x
d.Values = []adt.Disjunct{}
for _, v := range x.Values {
if v.Default {
d.Values = append(d.Values, v)
}
}
if len(d.Values) == 1 {
return d.Values[0].Val, true
}
return &d, true

case *adt.BinaryExpr:
if x.Op != adt.AndOp {
return x, false
}
a, sa := stripNonDefaults(x.X)
b, sb := stripNonDefaults(x.Y)
if sa || sb {
bin := *x
bin.X = a
bin.Y = b
return &bin, true
}
return x, false

default:
return x, false
}
}

// Label reports he label used to obtain this value from the enclosing struct.
Expand Down
8 changes: 5 additions & 3 deletions cue/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3324,10 +3324,12 @@ func TestPathCorrection(t *testing.T) {
if !i.Next() {
t.Fatal("no fields")
}
// Locate b
// Locate b, the second field
i, _ = i.Value().Fields(cue.Definitions(true), cue.Optional(true))
if !(i.Next() && i.Next()) {
t.Fatal("no fields")
for range 2 {
if !i.Next() {
t.Fatal("no fields")
}
}
v := i.Value()
return v
Expand Down

0 comments on commit 537f744

Please sign in to comment.