regex: fix false positive in case of nested classes

GitOrigin-RevId: c73091ff46baa78354ff8cea8455b5bb874ea57f
This commit is contained in:
Bas Leijdekkers
2020-04-02 15:58:48 +02:00
committed by intellij-monorepo-bot
parent e0895e2a54
commit cbd19c935f
3 changed files with 12 additions and 8 deletions

View File

@@ -1,6 +1,7 @@
<html>
<body>
Reports duplicate characters inside a RegExp character class. For example <code>[++]</code>.
Duplicate characters are unnecessary and can be removed without changing the semantics of the regex.
<!-- tooltip end -->
</body>
</html>

View File

@@ -34,8 +34,9 @@ public class DuplicateCharacterInClassInspection extends LocalInspectionTool {
@Override
public void visitRegExpClass(RegExpClass regExpClass) {
if (!(regExpClass.getParent() instanceof RegExpClass)) {
checkForDuplicates(regExpClass, new HashSet<>());
final HashSet<Object> seen = new HashSet<>();
for (RegExpClassElement element : regExpClass.getElements()) {
checkForDuplicates(element, seen);
}
}
@@ -59,12 +60,6 @@ public class DuplicateCharacterInClassInspection extends LocalInspectionTool {
new DuplicateCharacterInClassFix(regExpSimpleClass));
}
}
else if (element instanceof RegExpClass) {
final RegExpClass regExpClass = (RegExpClass)element;
for (RegExpClassElement classElement : regExpClass.getElements()) {
checkForDuplicates(classElement, seen);
}
}
}
}

View File

@@ -26,6 +26,14 @@ public class DuplicateCharacterInClassInspectionTest extends RegExpInspectionTes
public void testSpace() {
quickfixAllTest("[ <warning descr=\"Duplicate character ' ' inside character class\"><caret> </warning><warning descr=\"Duplicate character ' ' inside character class\"> </warning>]", "[ ]");
}
public void testNegatedClass() {
highlightTest("[^a<warning descr=\"Duplicate character 'a' inside character class\">a</warning>]");
}
public void testNestedClass() {
highlightTest("[<[^<>]*>]*<[^<>]*");
}
@Override
protected @NotNull LocalInspectionTool getInspection() {