A bracket expression in a pattern names one character more than once

tx0/java:regex-duplicate-class-memberrecommendedwarningsmell

What it finds

[a-zA-Z_0-9.-_] looks like a list but .-_ is a range that also covers the digits and upper case letters, and [\w_] repeats the underscore that \w holds. A character listed twice changes nothing, and is often the sign of a range that was meant as three characters, or of a different character that was meant.

Reported is a class, without intersections or nested classes, in which a character is listed twice, falls in a range that is also listed, or belongs to a listed \d, \w or \s. Patterns that ignore case are left alone.

From the rule's source, regex_duplicate_class_member.rs.

Example from the rule's tests
import java.util.regex.Pattern; class Classes {  Pattern a = Pattern.compile("[\\w_]+");tx0 reports: lists _ more than once  Pattern b = Pattern.compile("[a-zA-Z.-_]");tx0 reports: more than once  Pattern c = Pattern.compile("[a-zA-Z0-9_.-]");  Pattern d = Pattern.compile("[aA]", Pattern.CASE_INSENSITIVE);}
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 recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/java:regex-duplicate-class-member" = "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-duplicate-class-member] reason="..."

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