A pattern refers back to a capture that does not exist yet at that point

tx0/kotlin:regex-unmatched-backreferencestricterrorreliability/strings-and-regexdepth fastbug

What it finds

(a)\2 refers to a second group that does not exist, and \1(a) to a group that has not matched yet when the reference is reached: such a reference never matches, so the whole alternative never does. Pattern reads a back reference by number without checking that the group exists before it.

Reported is a numbered back reference to a group that is not opened before it in the pattern.

From the rule's source, regex_patterns.rs.

Example from the rule's tests
val twice = Regex("(a)\\2")tx0 reports: refers to a group that is not defined before itval early = "\\1(a)".toRegex()tx0 reports: never matchesval fine = Regex("(a)\\1")
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-unmatched-backreference" = "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-unmatched-backreference] reason="..."

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