Skip to content

avoid-unicode-escape

Javastyleno fixdefault: offjava/google

Reports a string or character literal that writes a visible character as a \uXXXX escape when the line has no trailing comment. Escapes of control, space and format characters are not reported.

The escape hides the character: the reader has to decode é to know it is an é, and cannot see a typo in the code. The source file is UTF-8, so the character itself is safe. An escape stays useful when a comment explains it, which is why a comment on the line allows it.

To fix, write the character itself, or add a trailing comment that says what it is.

Reported
class A {
String a = "\u00e9";
String b = "\u00e9"; // c with acute
String c = "\u0009\u200b";
char d = '\u2603';
String e = "plain";
}
java:
lint:
rules:
avoid-unicode-escape: warn # off (default), warn, error

Enabled by the presets: java/google.

Suppress one line with sudsy-disable-next-line avoid-unicode-escape in a comment of the file’s language.

The same check in other tools. full: Sudsy reports what the upstream rule reports; partial: only part of it, as the last column says.

Tool Rule Match What differs
checkstyle AvoidEscapedUnicodeCharacters partial Printable-escape rule with control characters and tail comments allowed; no options.