Java: fix triple quote escape handling

in "Unnecessarily escaped character" inspection

GitOrigin-RevId: 662d5aacf82fadc8a6e980a350200e797d7f7828
This commit is contained in:
Bas Leijdekkers
2024-10-21 21:50:02 +00:00
committed by intellij-monorepo-bot
parent 20553eb629
commit 88a54093fe
4 changed files with 20 additions and 4 deletions
@@ -181,10 +181,11 @@ public final class UnnecessaryStringEscapeInspection extends BaseInspection impl
else if (ch == '"' && doubleQuotes < 2) {
if (i == end - 1) return -1;
if (i == end - 2) return i - 1;
if (i < end - 2 && text.charAt(i + 1) == '"') {
if (doubleQuotes != 1 && text.charAt(i + 2) != '"') return i - 1;
if (doubleQuotes == 1) {
if (!text.startsWith("\"", i + 1) && !text.startsWith("\\\"", i + 1)) return i - 1;
}
else {
else if (!text.startsWith("\"\"", i + 1) && !text.startsWith("\\\"\"", i + 1) &&
!text.startsWith("\"\\\"", i + 1) && !text.startsWith("\\\"\\\"", i + 1)) {
return i - 1;
}
}
@@ -0,0 +1,7 @@
class NestedTextBlock {
String s = """
String s = \"<caret>""
test
\""";
""";
}
@@ -0,0 +1,7 @@
class NestedTextBlock {
String s = """
String s = \"<warning descr="'\\\\"' is unnecessarily escaped"><caret>\"</warning><warning descr="'\\\\"' is unnecessarily escaped">\"</warning>
test
\"<warning descr="'\\\\"' is unnecessarily escaped">\"</warning><warning descr="'\\\\"' is unnecessarily escaped">\"</warning>;
""";
}
@@ -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-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.siyeh.ig.redundancy;
import com.intellij.codeInspection.InspectionProfileEntry;
@@ -20,6 +20,7 @@ public class UnnecessaryStringEscapeInspectionTest extends LightJavaInspectionTe
public void testEscapedNewLine() { doQuickFixTest(); }
public void testStringTemplate1() { doQuickFixTest(); }
public void testStringTemplate2() { doQuickFixTest(); }
public void testNestedTextBlock() { doQuickFixTest(); }
public void testEscapedNewLineNotUnnecessary() { doTest(); }
public void testBrokenCode() { doTest(); }