RedundantStringEscape

SUGGESTION

Simplification

View source code on GitHub

Summary

Inside string expressions single quotes do not need to be escaped

Suppression

Suppress false positives by adding the suppression annotation @SuppressWarnings("RedundantStringEscape") to the enclosing element.

Disable this pattern completely by adding -Xep:RedundantStringEscape:OFF as compiler argument. Learn more.

Samples

Replacement

Shows the difference in example code before and after the bug pattern is applied.

 import java.util.Arrays;
 import java.util.List;
 
 class A {
   List<String> m() {
     return Arrays.asList(
-        "\'",
-        "'\'",
-        "\''",
-        "\'\'",
-        "\'ß\'",
+        "'",
+        "''",
+        "''",
+        "''",
+        "'ß'",
         (
-        /* Leading comment. */ "\'" /* Trailing comment. */),
-        "\'foo\"bar\'baz\"qux\'");
+        /* Leading comment. */ "'" /* Trailing comment. */),
+        "'foo\"bar'baz\"qux'");
   }
 }
 

Identification

Shows code lines which will (not) be flagged by this bug pattern.
A //BUG: Diagnostic contains: comment is placed above any violating line.

import java.util.Arrays;
import java.util.List;

class A {
  List<String> m() {
    return Arrays.asList(
        "foo",
        "ß",
        "'",
        "\"",
        "\\",
        "\\'",
        "'\\",
        // BUG: Diagnostic contains:
        "\\\'",
        // BUG: Diagnostic contains:
        "\'\\",
        // BUG: Diagnostic contains:
        "\'",
        // BUG: Diagnostic contains:
        "'\'",
        // BUG: Diagnostic contains:
        "\''",
        // BUG: Diagnostic contains:
        "\'\'",
        (
        // BUG: Diagnostic contains:
        /* Leading comment. */ "\'" /* Trailing comment. */),
        // BUG: Diagnostic contains:
        "\'foo\"bar\'baz\"qux\'");
  }
}

Copyright © 2017-2024 Picnic Technologies BV