From 0498719d5422843967a4d31bd686f9717dcc290e Mon Sep 17 00:00:00 2001 From: Bas Leijdekkers Date: Tue, 24 Jun 2025 15:42:39 +0200 Subject: [PATCH] RegExp: warn about duplicate char in character class with intersection (IJPL-193556) for "Duplicate character in character class" inspection GitOrigin-RevId: 455aff90e2c52c735df582fced65225b5ecbb2f0 --- .../inspection/DuplicateCharacterInClassInspection.java | 8 +++++++- .../DuplicateCharacterInClassInspectionTest.java | 7 ++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/RegExpSupport/src/org/intellij/lang/regexp/inspection/DuplicateCharacterInClassInspection.java b/RegExpSupport/src/org/intellij/lang/regexp/inspection/DuplicateCharacterInClassInspection.java index fed9dad0ad45..c34387d2697d 100644 --- a/RegExpSupport/src/org/intellij/lang/regexp/inspection/DuplicateCharacterInClassInspection.java +++ b/RegExpSupport/src/org/intellij/lang/regexp/inspection/DuplicateCharacterInClassInspection.java @@ -1,4 +1,4 @@ -// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. +// Copyright 2000-2025 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. package org.intellij.lang.regexp.inspection; import com.intellij.codeInspection.LocalInspectionTool; @@ -63,6 +63,12 @@ public class DuplicateCharacterInClassInspection extends LocalInspectionTool { new DuplicateCharacterInClassFix(regExpSimpleClass)); } } + else if (element instanceof RegExpIntersection intersection) { + final HashSet visited = new HashSet<>(); + for (RegExpClassElement operand : intersection.getOperands()) { + checkForDuplicates(operand, visited); + } + } } } diff --git a/RegExpSupport/test/org/intellij/lang/regexp/inspection/DuplicateCharacterInClassInspectionTest.java b/RegExpSupport/test/org/intellij/lang/regexp/inspection/DuplicateCharacterInClassInspectionTest.java index 0e4042b96e2a..c0be4607e6f4 100644 --- a/RegExpSupport/test/org/intellij/lang/regexp/inspection/DuplicateCharacterInClassInspectionTest.java +++ b/RegExpSupport/test/org/intellij/lang/regexp/inspection/DuplicateCharacterInClassInspectionTest.java @@ -1,4 +1,4 @@ -// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. +// Copyright 2000-2025 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. package org.intellij.lang.regexp.inspection; import com.intellij.codeInspection.LocalInspectionTool; @@ -8,6 +8,7 @@ import org.jetbrains.annotations.NotNull; /** * @author Bas Leijdekkers */ +@SuppressWarnings("RegExpDuplicateCharacterInClass") public class DuplicateCharacterInClassInspectionTest extends RegExpInspectionTestCase { public void testPredefinedCharacterClass() { @@ -34,6 +35,10 @@ public class DuplicateCharacterInClassInspectionTest extends RegExpInspectionTes public void testNestedClass() { highlightTest("[<[^<>]*>]*<[^<>]*"); } + + public void testIntersection() { + highlightTest("[\\w\\w&&[^_]]"); + } @Override protected @NotNull LocalInspectionTool getInspection() {