From 537f744a9cc718ae4017be24e962ed7a68a334c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Mart=C3=AD?= Date: Tue, 15 Oct 2024 14:08:16 +0100 Subject: [PATCH] cue: resolve a few staticcheck warnings MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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í Change-Id: If6608b5d9d1076316e3c03e530b6b6577a157605 Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1202602 Reviewed-by: Roger Peppe TryBot-Result: CUEcueckoo Unity-Result: CUE porcuepine --- cue/instance.go | 11 ---- cue/types.go | 127 ---------------------------------------------- cue/types_test.go | 8 +-- 3 files changed, 5 insertions(+), 141 deletions(-) diff --git a/cue/instance.go b/cue/instance.go index cea6ffc33a6..35b713390da 100644 --- a/cue/instance.go +++ b/cue/instance.go @@ -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 { diff --git a/cue/types.go b/cue/types.go index d2704b2e5d6..7f246a81cff 100644 --- a/cue/types.go +++ b/cue/types.go @@ -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. diff --git a/cue/types_test.go b/cue/types_test.go index 76c16da754d..18f89c4e78d 100644 --- a/cue/types_test.go +++ b/cue/types_test.go @@ -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