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

tx0/java:regex-unmatchable-anchordefaulterrorbug

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 flag ^ 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_unmatchable_anchor.rs.

Example from the rule's tests
import java.util.regex.Pattern; class Anchors {  Pattern a = Pattern.compile("a^b");tx0 reports: can never match  Pattern b = Pattern.compile("end$x");tx0 reports: can never match  Pattern c = Pattern.compile("a^b", Pattern.MULTILINE);  Pattern d = Pattern.compile("end$\\n?");  Pattern e = Pattern.compile("(?:^|,)x");}
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 in every tier. To turn it off for a project, set its level in tx0.toml:

tx0.toml
[rules]
"tx0/java:regex-unmatchable-anchor" = "off"

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/java:regex-unmatchable-anchor] reason="..."

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