A constant regular expression is compiled inside a loop, so the same pattern is parsed on every pass
What it finds
Pattern.compile parses a pattern, and String.matches, replaceAll and split with more than a plain character do too, on every call. A constant pattern compiled once into a static final Pattern costs nothing per pass.
Reported is such a call with a constant pattern inside a loop.
From the rule's source, regex_compiled_in_loop.rs.
import java.util.List;import java.util.regex.Pattern; class Loops { void run(List<String> lines) { for (String line : lines) { boolean a = line.matches("\\d+");tx0 reports: compiled again String[] b = line.split(","); } }} Turn it on, off or suppress it
The rule runs from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:
[rules]
"tx0/java:regex-compiled-in-loop" = "warning"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-compiled-in-loop] reason="..."tx0 explain tx0/java:regex-compiled-in-loop shows the same in the terminal. Found a false positive? Report it.