Since C++11, raw string literals can be used to avoid the need to escape characters in a string.
This rules raises an issue when using a raw string literal would make a string easier to read. For instance, when a non-raw string contains
different escaped sequences (among \'
, \\
, \"
and \?
) or more than two of the same kind.
Noncompliant code example
const auto* result = "a\?b \""; // Noncompliant
const auto* regEx = "\\\\(\\\\.\\\\)"; // Noncompliant
const auto* message = "Use \"x\" or \"y\""; // Noncompliant
const auto* rawLit = R"(Hello!)" ...)"; // Noncompliant, literal contains closing sequence
Compliant solution
const auto* result = R"(a?b ")";
const auto* regEx = R"(\\(\\.\\))";
const auto* message = R"(Use "x" or "y")";
const auto* rawLit = R"_(Hello!)" ...)_"; // Compliant, uses delimiter whose closing sequence does not appear in the literal
const auto* twoLines = "one\r\ntwo"; // Compliant, contains \r
const auto* path = "C:\\Program Files\\Microsoft Office\\Office16\\"; // Compliant, raw strings would not improve readability
Exceptions
To preserve readability, this rule ignores strings containing only one character and strings with escaped whitespace or non-printable
characters:
- Non-printable characters:
\a
\b
\f
\v
\nnn
\xnn
\unnnn
\Unnnnnnnn
- Tab:
\t
- Carriage return:
\r