From 3a8dccfbc7c255fdca6e552d0a17323f2a02dd31 Mon Sep 17 00:00:00 2001 From: Bas Leijdekkers Date: Thu, 10 Apr 2025 11:45:56 +0200 Subject: [PATCH] RegExp: fix false positive on class intersection (IJPL-102098) in "Regular expression can be simplified" inspection GitOrigin-RevId: 91eafa57c5e1a1e5df8fd1f8e3b0fb1644891e04 --- .../regexp/inspection/RegExpSimplifiableInspection.java | 4 ++-- .../inspection/RegExpSimplifiableInspectionTest.java | 7 ++++++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/RegExpSupport/src/org/intellij/lang/regexp/inspection/RegExpSimplifiableInspection.java b/RegExpSupport/src/org/intellij/lang/regexp/inspection/RegExpSimplifiableInspection.java index 6e175fc47feb..4c0004233d68 100644 --- a/RegExpSupport/src/org/intellij/lang/regexp/inspection/RegExpSimplifiableInspection.java +++ b/RegExpSupport/src/org/intellij/lang/regexp/inspection/RegExpSimplifiableInspection.java @@ -1,4 +1,4 @@ -// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. +// 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.*; @@ -62,7 +62,7 @@ public class RegExpSimplifiableInspection extends LocalInspectionTool { if (elements.length == 1) { final RegExpClassElement element = elements[0]; if (element instanceof RegExpPosixBracketExpression) return; - if (!(element instanceof RegExpCharRange)) { + if (!(element instanceof RegExpCharRange) && !(element instanceof RegExpIntersection)) { if (!(element instanceof RegExpChar) || !"{}().*+?|$".contains(element.getText())) { // [a] -> a registerProblem(regExpClass, element.getUnescapedText()); diff --git a/RegExpSupport/test/org/intellij/lang/regexp/inspection/RegExpSimplifiableInspectionTest.java b/RegExpSupport/test/org/intellij/lang/regexp/inspection/RegExpSimplifiableInspectionTest.java index 9ac0cb2a8c4e..558f84d7172e 100644 --- a/RegExpSupport/test/org/intellij/lang/regexp/inspection/RegExpSimplifiableInspectionTest.java +++ b/RegExpSupport/test/org/intellij/lang/regexp/inspection/RegExpSimplifiableInspectionTest.java @@ -1,4 +1,4 @@ -// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. +// 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.CommonQuickFixBundle; @@ -37,6 +37,11 @@ public class RegExpSimplifiableInspectionTest extends RegExpInspectionTestCase { highlightTest("[0-9abc]"); // no warn; replacing with \d is not equivalent in Unicode context } + public void testIntersection() { + highlightTest("^[\\p{ASCII}&&[^@]]+@[\\p{ASCII}&&[^@]]+$"); + highlightTest("[\\W&&\\S]"); + } + public void testSingleElementClass() { doTest("[a]", "a"); }