Inspectopedia
 
2024.3

Non-terminal use of '\s' escape sequence

Warning
New
Last modified: 03 December 2024

Reports \s escape sequences anywhere except at text-block line endings or within a series of several escaped spaces. Such usages can be confusing or a mistake, especially if the string is interpreted as a regular expression. The \s escape sequence is intended to encode a space at the end of text-block lines where normal spaces are trimmed. In other locations, as well as in regular string or char literals, \s is identical to an ordinary space character (" ").

Example:

Here it's likely that "\\s+" was intended (to match any whitespace character). If not, using str.matches(" +") would be less confusing.

A quick-fix is provided that replaces \s escapes with space characters.

This inspection depends on the Java feature ''\s' escape sequences', which is available since Java 15.

New in 2022.3