Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ public void test() {
f(compile("(?=a)ab").matcher("abc").find());
f(compile("(?=abc)ab").matcher("abc").matches()); // Noncompliant
f(compile("(?=abc)ab").matcher("abc").find());

// string regex and pattern regex share the same string literal
String pattern_str = "(?=a)b"; // Noncompliant
Pattern pattern = Pattern.compile(pattern_str);
f("".matches(pattern_str));
f(pattern.matcher("").matches());
}

abstract void f(Pattern pattern);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ private Optional<RegexParseResult> getRegexOperand(MethodInvocationTree mit) {
@Override
public void checkRegex(RegexParseResult regexForLiterals, ExpressionTree methodInvocationOrAnnotation) {
if (methodInvocationOrAnnotation.is(Tree.Kind.ANNOTATION)) {
methodsCalledOnRegex.put(regexForLiterals, Collections.emptyList());
methodsCalledOnRegex.put(regexForLiterals, new ArrayList<>());
return;
}
MethodInvocationTree mit = (MethodInvocationTree) methodInvocationOrAnnotation;
Expand All @@ -215,9 +215,9 @@ public void checkRegex(RegexParseResult regexForLiterals, ExpressionTree methodI
methodInvocationToRegex.put(mit, regexForLiterals);
methodsCalledOnRegex.put(regexForLiterals, new ArrayList<>());
} else if (trackedMethodMatchers().matches(mit)) {
methodsCalledOnRegex.put(regexForLiterals, Collections.singletonList(mit));
methodsCalledOnRegex.put(regexForLiterals, new ArrayList<>(Collections.singletonList(mit)));
} else {
methodsCalledOnRegex.put(regexForLiterals, Collections.emptyList());
methodsCalledOnRegex.put(regexForLiterals, new ArrayList<>());
}
}

Expand Down