From aaa9392f8b1e4a2af63e46dfd64a2184a39d338b Mon Sep 17 00:00:00 2001 From: Tagir Valeev Date: Mon, 25 Jul 2022 16:11:56 +0200 Subject: [PATCH] [java-inspections] IDEA-298388 "Java | Code style issues | Non-terminal use of '\s' escape sequence" false positive GitOrigin-RevId: 3e7989a3bf971e7b6be613abee893cbe334dde31 --- .../escapedSpace/beforeSlashSlash.java | 8 +++++ .../ig/style/EscapedSpaceInspection.java | 33 ++++++++++++++----- 2 files changed, 33 insertions(+), 8 deletions(-) create mode 100644 java/java-tests/testData/codeInspection/escapedSpace/beforeSlashSlash.java diff --git a/java/java-tests/testData/codeInspection/escapedSpace/beforeSlashSlash.java b/java/java-tests/testData/codeInspection/escapedSpace/beforeSlashSlash.java new file mode 100644 index 000000000000..5296ee328be6 --- /dev/null +++ b/java/java-tests/testData/codeInspection/escapedSpace/beforeSlashSlash.java @@ -0,0 +1,8 @@ +// "Replace with a space" "false" +class X { + void test(String str) { + if (str.matches("\\s+")) { + + } + } +} \ No newline at end of file diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/style/EscapedSpaceInspection.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/style/EscapedSpaceInspection.java index 8f7780a9092a..38a334131c04 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/style/EscapedSpaceInspection.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/style/EscapedSpaceInspection.java @@ -22,16 +22,33 @@ public class EscapedSpaceInspection extends AbstractBaseJavaLocalInspectionTool if (!TypeUtils.isJavaLangString(type)) return; boolean block = literal.isTextBlock(); String text = literal.getText(); - int pos = 0; + int pos = 1; while (true) { - pos = text.indexOf("\\s", pos + 1); - if (pos == -1) return; - if (pos > 2 && text.startsWith("\\s", pos - 2)) continue; - if (text.startsWith("\\s", pos + 2)) continue; - if (block && (pos + 2 == text.length() || text.charAt(pos + 2) == '\n')) continue; - holder.registerProblem(literal, TextRange.create(pos, pos+2), + pos = text.indexOf('\\', pos); + if (pos == -1 || pos == text.length() - 1) return; + char next = text.charAt(pos + 1); + if (next == 'u') { + // unicode escape + pos += 6; + continue; + } + pos += 2; + if (next >= '0' && next <= '9') { + // octal escape + if (pos < text.length() && text.charAt(pos) >= '0' && text.charAt(pos) <= '9') pos++; + if (pos < text.length() && text.charAt(pos) >= '0' && text.charAt(pos) <= '9') pos++; + continue; + } + if (next != 's') { + // other escapes + continue; + } + if (pos > 4 && text.startsWith("\\s", pos - 4)) continue; + if (text.startsWith("\\s", pos)) continue; + if (block && (pos == text.length() || text.charAt(pos) == '\n')) continue; + holder.registerProblem(literal, TextRange.create(pos - 2, pos), InspectionGadgetsBundle.message("inspection.use.of.slash.s.message"), - new ReplaceWithSpaceFix(pos)); + new ReplaceWithSpaceFix(pos - 2)); } } };