A ^ or $ in a regular expression stands where it can never match, so the pattern never matches

tx0/kotlin:regex-unmatchable-anchorstricterrorreliability/strings-and-regexdepth fastbug

What it finds

a^b can never match: ^ matches at the start of the input, where no a can come before it. Likewise $ matches only at the end, or before a final line terminator, so $b followed by a character other than a line terminator never matches, and \A and \z mark the very start and end. With the MULTILINE option ^ and $ also match at line breaks, and are left alone.

Reported is ^ or \A after something that always consumes a character, and $, \Z or \z before something that always consumes a character other than a line terminator, within one sequence of the pattern.

From the rule's source, regex_patterns.rs.

Example from the rule's tests
val never = Regex("a^b")tx0 reports: an anchor of this regular expression stands where it can never matchval end = Regex("a\$b")tx0 reports: can never matchval lines = Regex("a$\\n^b", RegexOption.MULTILINE)val fine = Regex("^ab$")
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

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:

tx0.toml
[rules]
"tx0/kotlin:regex-unmatchable-anchor" = "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:regex-unmatchable-anchor] reason="..."

tx0 explain tx0/kotlin:regex-unmatchable-anchor shows the same in the terminal. Found a false positive? Report it.