Back to Opa

var-shadows-builtin

docs/projects/regal/rules/bugs/var-shadows-builtin.md

1.16.11.4 KB
Original Source

var-shadows-builtin

Summary: Variable shadows built-in

Category: Bugs

Avoid

rego
package policy

# variable `http` shadows `http.send` built-in function
allow if {
    http := startswith(input.url, "http://")
    # do something with http
}

Prefer

rego
package policy

# variable `is_http` doesn't shadow any built-in function
allow if {
    is_http := startswith(input.url, "http://")
    # do something with is_http
}

Rationale

Using the name of built-in functions or operators as variable names can lead to confusion and unexpected behavior. A variable that shadows a built-in function (or the namespace of a function, like http in http.send) prevents any function in that namespace to be used later in the rule. Avoid this!

Configuration Options

This linter rule provides the following configuration options:

yaml
rules:
  bugs:
    var-shadows-builtin:
      # one of "error", "warning", "ignore"
      level: error