A regular expression literal is malformed, so compiling it throws
What it finds
Regex("[a-z"), "(".toRegex() and Pattern.compile("{name}") throw a PatternSyntaxException the first time they run: the pattern is not valid in the syntax of java.util.regex.Pattern, which Kotlin uses on the JVM. Common causes are unescaped parentheses, brackets and braces, quantifiers with nothing to repeat, ranges that run backwards and escapes that Pattern does not define.
Reported is a pattern written as a string literal without templates, or a concatenation of such literals, given to Regex, toRegex, toPattern or Pattern.compile.
From the rule's source, regex_patterns.rs.
import java.util.regex.Pattern val a = Regex("[a-z")tx0 reports: this regular expression does not compileval b = "(".toRegex()tx0 reports: does not compileval c = Pattern.compile("x{2,1}")tx0 reports: does not compileval d = Regex("[a-z]+")val e = Regex("$prefix(")val f = "a+".toRegex(RegexOption.IGNORE_CASE)val g = Regex("(", RegexOption.LITERAL) Turn it on, off or suppress it
The rule runs from the strict tier up. To turn it on in a lower tier, give it a level in tx0.toml:
[rules]
"tx0/kotlin:invalid-regex" = "error"To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:
// tx0: ignore[tx0/kotlin:invalid-regex] reason="..."tx0 explain tx0/kotlin:invalid-regex shows the same in the terminal. Found a false positive? Report it.