Skip to main content

non-raw-regex-pattern

Summary: Use raw strings for regex patterns

Category: Idiomatic

Automatically fixable: Yes

Avoid

all_digits if {
regex.match("[\\d]+", "12345")
}

Prefer

all_digits if {
regex.match(`[\d]+`, "12345")
}

Rationale​

Raw strings are interpreted literally, allowing you to avoid having to escape special characters like \ in your regex patterns. Using raw strings for regex patterns additionally makes them easier to identify as such.

Limitations​

This rule currently only scans regex string literals in the place of the pattern argument of the various regex built-in functions. It will not not try to "resolve" patterns assigned to variables. The following example would as such not render a warning:

package policy

# Pattern assigned to variable
pattern := "[\\d]+"

# This won't trigger a violation
allow if regex.match(pattern, "12345")

Configuration Options​

This linter rule provides the following configuration options:

rules:
idiomatic:
non-raw-regex-pattern:
# one of "error", "warning", "ignore"
level: error