mirror of
https://github.com/open-policy-agent/opa.git
synced 2026-08-12 19:32:48 -06:00
Various style fixes
Submitting some miscellaneous changes I had locally. A few allocs saved, but mostly style fixes here, like simplifying known var/var equality using `==` and so on. Nothing controversial, or so I'd like to think :) Signed-off-by: Anders Eknert <anders.eknert@apple.com>
This commit is contained in:
committed by
Stephan Renatus
parent
cf1d96ab49
commit
64b079dea9
@@ -148,7 +148,7 @@ func ShortsFromMappings(mappings map[string]any) Set[string] {
|
||||
// number of rules present on the compiler. It should be used only for
|
||||
// error messages.
|
||||
func FuzzyRuleNameMatchHint(comp *ast.Compiler, input string) string {
|
||||
rules := comp.GetRules(ast.Ref{ast.DefaultRootDocument})
|
||||
rules := comp.GetRules(ast.DefaultRootRef)
|
||||
ruleNames := make([]string, 0, len(rules))
|
||||
for _, rule := range rules {
|
||||
if rule.Default {
|
||||
|
||||
+2
-2
@@ -1238,8 +1238,8 @@ func removeDuplicate(list []Value) []Value {
|
||||
return newResult
|
||||
}
|
||||
|
||||
func getArgTypes(env *TypeEnv, args []*Term) []types.Type {
|
||||
pre := make([]types.Type, len(args))
|
||||
func getArgTypes(env *TypeEnv, args []*Term) (pre []types.Type) {
|
||||
pre = make([]types.Type, len(args))
|
||||
for i := range args {
|
||||
pre[i] = env.GetByValue(args[i].Value)
|
||||
}
|
||||
|
||||
+5
-5
@@ -2235,7 +2235,7 @@ func (c *Compiler) resolveAllRefs() {
|
||||
}
|
||||
|
||||
for v, u := range globals {
|
||||
if v.Equal(imp.Name()) && !u.used {
|
||||
if v == imp.Name() && !u.used {
|
||||
if !c.err(NewError(CompileErr, imp.Location, "%s unused", imp.String())) {
|
||||
return
|
||||
}
|
||||
@@ -6535,7 +6535,7 @@ func (s localDeclaredVars) Insert(x, y Var, occurrence varOccurrence) {
|
||||
// If the variable has been rewritten (where x != y, with y being
|
||||
// the generated value), store it in the map of rewritten vars.
|
||||
// Assume that the generated values are unique for the compilation.
|
||||
if !x.Equal(y) {
|
||||
if x != y {
|
||||
s.rewritten[y] = x
|
||||
}
|
||||
}
|
||||
@@ -7013,7 +7013,7 @@ func rewriteDeclaredVarsInTerm(g *localVarGenerator, stack *localDeclaredVars, t
|
||||
case Call:
|
||||
ref := v[0]
|
||||
WalkVars(ref, func(v Var) bool {
|
||||
if gv, ok := stack.Declared(v); ok && !gv.Equal(v) {
|
||||
if gv, ok := stack.Declared(v); ok && gv != v {
|
||||
// We will rewrite the ref of a function call, which is never ok since we don't have first-class functions.
|
||||
errs = append(errs, NewError(CompileErr, term.Location, "called function %s shadowed", ref))
|
||||
return true
|
||||
@@ -7068,11 +7068,11 @@ func rewriteDeclaredVarsInWithRecursive(g *localVarGenerator, stack *localDeclar
|
||||
if sdwInput, ok := stack.Declared(InputRootDocument.Value.(Var)); ok { // Was "input" shadowed...
|
||||
switch value := w.Target.Value.(type) {
|
||||
case Var:
|
||||
if sdwInput.Equal(value) { // ...and replaced? If so, fix it
|
||||
if sdwInput == value { // ...and replaced? If so, fix it
|
||||
w.Target.Value = InputRootRef
|
||||
}
|
||||
case Ref:
|
||||
if sdwInput.Equal(value[0].Value.(Var)) {
|
||||
if sdwInput.Equal(value[0].Value) {
|
||||
w.Target.Value.(Ref)[0].Value = InputRootDocument.Value
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12794,11 +12794,11 @@ func TestKeepModules(t *testing.T) {
|
||||
}
|
||||
newName := Var("q")
|
||||
mods["bar.rego"].Rules[0].Head.Name = newName
|
||||
if exp, act := newName, mod.Rules[0].Head.Name; !exp.Equal(act) {
|
||||
if exp, act := newName, mod.Rules[0].Head.Name; exp != act {
|
||||
t.Errorf("expected modified rule name %v, found %v", exp, act)
|
||||
}
|
||||
mods["extra.rego"].Rules[0].Head.Name = newName
|
||||
if exp, act := newName, extra.Rules[0].Head.Name; !exp.Equal(act) {
|
||||
if exp, act := newName, extra.Rules[0].Head.Name; exp != act {
|
||||
t.Errorf("expected modified rule name %v, found %v", exp, act)
|
||||
}
|
||||
})
|
||||
|
||||
@@ -184,13 +184,7 @@ func (r *matchResult) addRef(loc *ast.Location) {
|
||||
|
||||
// termMatchesVar checks if a term contains a variable matching the given name.
|
||||
func termMatchesVar(t *ast.Term, name ast.Var) bool {
|
||||
if t == nil {
|
||||
return false
|
||||
}
|
||||
|
||||
v, ok := t.Value.(ast.Var)
|
||||
|
||||
return ok && v.Equal(name)
|
||||
return t != nil && name.Equal(t.Value)
|
||||
}
|
||||
|
||||
// findRulesDefinition looks up rules for a given ref. Rules appear in various
|
||||
|
||||
+3
-23
@@ -402,7 +402,7 @@ func (mod *Module) String() string {
|
||||
func (mod *Module) RuleSet(name Var) RuleSet {
|
||||
rs := NewRuleSet()
|
||||
for _, rule := range mod.Rules {
|
||||
if rule.Head.Name.Equal(name) {
|
||||
if rule.Head.Name == name {
|
||||
rs.Add(rule)
|
||||
}
|
||||
}
|
||||
@@ -1081,19 +1081,7 @@ func (body Body) Set(expr *Expr, pos int) {
|
||||
//
|
||||
// If body is a subset of other, it is considered less than (and vice versa).
|
||||
func (body Body) Compare(other Body) int {
|
||||
minLen := min(len(other), len(body))
|
||||
for i := range minLen {
|
||||
if cmp := body[i].Compare(other[i]); cmp != 0 {
|
||||
return cmp
|
||||
}
|
||||
}
|
||||
if len(body) < len(other) {
|
||||
return -1
|
||||
}
|
||||
if len(other) < len(body) {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
return slices.CompareFunc(body, other, (*Expr).Compare)
|
||||
}
|
||||
|
||||
// Copy returns a deep copy of body.
|
||||
@@ -2189,14 +2177,6 @@ func isGlobalBuiltin(expr *Expr, name Var) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// NOTE(tsandall): do not use Term#Equal or Value#Compare to avoid
|
||||
// allocation here.
|
||||
ref, ok := terms[0].Value.(Ref)
|
||||
if !ok || len(ref) != 1 {
|
||||
return false
|
||||
}
|
||||
if head, ok := ref[0].Value.(Var); ok {
|
||||
return head.Equal(name)
|
||||
}
|
||||
return false
|
||||
return ok && len(ref) == 1 && name.Equal(ref[0].Value)
|
||||
}
|
||||
|
||||
@@ -1348,8 +1348,7 @@ func TestWriterRejectsMixedPlanManifestFormats(t *testing.T) {
|
||||
}},
|
||||
}
|
||||
b.SetManifestProto(true)
|
||||
var buf bytes.Buffer
|
||||
err := NewWriter(&buf).Write(b)
|
||||
err := NewWriter(io.Discard).Write(b)
|
||||
if err == nil || !strings.Contains(err.Error(), "proto manifest but JSON plan") {
|
||||
t.Fatalf("expected mixed-format error, got: %v", err)
|
||||
}
|
||||
@@ -1362,8 +1361,7 @@ func TestWriterRejectsMixedPlanManifestFormats(t *testing.T) {
|
||||
Path: PlanProtoFile, URL: PlanProtoFile, Raw: []byte{0x00},
|
||||
}},
|
||||
}
|
||||
var buf bytes.Buffer
|
||||
err := NewWriter(&buf).Write(b)
|
||||
err := NewWriter(io.Discard).Write(b)
|
||||
if err == nil || !strings.Contains(err.Error(), "JSON manifest but proto plan") {
|
||||
t.Fatalf("expected mixed-format error, got: %v", err)
|
||||
}
|
||||
@@ -1376,8 +1374,7 @@ func TestWriterRejectsMixedPlanManifestFormats(t *testing.T) {
|
||||
Path: PlanProtoFile, URL: PlanProtoFile, Raw: []byte{0x00},
|
||||
}},
|
||||
}
|
||||
var buf bytes.Buffer
|
||||
if err := NewWriter(&buf).Write(b); err != nil {
|
||||
if err := NewWriter(io.Discard).Write(b); err != nil {
|
||||
t.Fatalf("expected empty-manifest write to succeed, got: %v", err)
|
||||
}
|
||||
})
|
||||
@@ -1390,8 +1387,7 @@ func TestWriterRejectsMixedPlanManifestFormats(t *testing.T) {
|
||||
}},
|
||||
}
|
||||
b.SetManifestProto(true)
|
||||
var buf bytes.Buffer
|
||||
if err := NewWriter(&buf).Write(b); err != nil {
|
||||
if err := NewWriter(io.Discard).Write(b); err != nil {
|
||||
t.Fatalf("expected uniform-proto write to succeed, got: %v", err)
|
||||
}
|
||||
})
|
||||
|
||||
+1
-1
@@ -788,7 +788,7 @@ func (r *REPL) unsetRule(ctx context.Context, name ast.Var) (bool, error) {
|
||||
rules := []*ast.Rule{}
|
||||
|
||||
for _, r := range mod.Rules {
|
||||
if !r.Head.Name.Equal(name) {
|
||||
if r.Head.Name != name {
|
||||
rules = append(rules, r)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -261,11 +261,10 @@ func (t bindingPlugTransform) Transform(x any) (any, error) {
|
||||
}
|
||||
|
||||
func (bindingPlugTransform) plugBindingsVar(pctx *plugContext, v ast.Var) ast.Value {
|
||||
|
||||
var result ast.Value = v
|
||||
|
||||
// Apply union-find to remove redundant variables from input.
|
||||
root, ok := pctx.uf.Find(v)
|
||||
root, ok := pctx.uf.Find(result)
|
||||
if ok {
|
||||
result = root.Value()
|
||||
}
|
||||
@@ -275,7 +274,7 @@ func (bindingPlugTransform) plugBindingsVar(pctx *plugContext, v ast.Var) ast.Va
|
||||
if !ok {
|
||||
return result
|
||||
}
|
||||
b := pctx.removedEqs.Get(v)
|
||||
b := pctx.removedEqs.Get(result)
|
||||
if b == nil {
|
||||
return result
|
||||
}
|
||||
|
||||
+1
-1
@@ -3383,7 +3383,7 @@ func (e evalVirtualPartial) partialEvalSupportRule(rule *ast.Rule, _ ast.Ref) (b
|
||||
head.Key = ruleRef[len(ruleRef)-1]
|
||||
}
|
||||
|
||||
if head.Name.Equal(ast.Var("")) && (len(ruleRef) == 1 || (len(ruleRef) == 2 && rule.Head.RuleKind() == ast.SingleValue)) {
|
||||
if head.Name == "" && (len(ruleRef) == 1 || (len(ruleRef) == 2 && rule.Head.RuleKind() == ast.SingleValue)) {
|
||||
head.Name = ruleRef[0].Value.(ast.Var)
|
||||
}
|
||||
|
||||
|
||||
+2
-2
@@ -292,7 +292,7 @@ func (s *saveSupport) Exists(path ast.Ref) bool {
|
||||
if len(ruleRef) == 1 {
|
||||
name := ruleRef[0].Value.(ast.Var)
|
||||
for _, rule := range module.Rules {
|
||||
if rule.Head.Name.Equal(name) {
|
||||
if rule.Head.Name == name {
|
||||
return true
|
||||
}
|
||||
}
|
||||
@@ -580,7 +580,7 @@ func (i *inliningControl) DisabledVar(v ast.Var, ignoreInternal bool) bool {
|
||||
}
|
||||
|
||||
for _, frame := range i.disable {
|
||||
if (!frame.internal || !ignoreInternal) && frame.v.Equal(v) {
|
||||
if (!frame.internal || !ignoreInternal) && frame.v == v {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user