Rules / Style / unnecessary-string-escapes
unnecessary-string-escapes
Flags a backslash escape that has no effect on the string's value.
A backslash only matters before a character that is special in the current
string context: recognized escapes (\n, \t, \x, \u, and friends), the
active quote character, $, and \ itself. Escaping anything else — \a, or
\' inside a double-quoted string — produces the same character while adding
visual clutter and inviting confusion about whether an escape was intended.
Raw strings (r'...') are never analyzed since backslashes are literal there,
and triple-quoted strings are skipped to avoid the edge cases around escaping
runs of quotes. Remove the redundant backslash.
Invalid
example.dartdart
// Bad: backslash escapes that do nothing in the given string context.
void examples() {
final a = "\'";
final b = '\"';
final c = '\a';
final d = 'x\dy';
final e = "50\%";
final f = '\-';
print([a, b, c, d, e, f]);
}
Unnecessary escape; the backslash can be removed.
2void examples() {
3 final a = "\'";
∙
Unnecessary escape; the backslash can be removed.
3 final a = "\'";
4 final b = '\"';
∙
Unnecessary escape; the backslash can be removed.
4 final b = '\"';
5 final c = '\a';
∙
Unnecessary escape; the backslash can be removed.
5 final c = '\a';
6 final d = 'x\dy';
∙
Unnecessary escape; the backslash can be removed.
6 final d = 'x\dy';
7 final e = "50\%";
∙
Unnecessary escape; the backslash can be removed.
7 final e = "50\%";
8 final f = '\-';
∙
Valid
example.dartdart
// Good: every backslash escape is meaningful (or the string is raw).
void examples() {
final a = 'it\'s'; // escaping the delimiter quote is required
final b = "she said \"hi\""; // escaping the delimiter quote is required
final c = 'line1\nline2'; // \n is a newline
final d = 'tab\there'; // \t is a tab
final e = 'cost: \$5'; // \$ escapes interpolation
final f = 'back\\slash'; // \\ is a literal backslash
final g = r'\d+\w*'; // raw string — backslashes are literal, never flagged
final h = 'emoji \u{1F600}'; // \u is a unicode escape
final i = 'plain text';
print([a, b, c, d, e, f, g, h, i]);
}
How to configure
Set the severity of unnecessary-string-escapes in your falcon.json:
falcon.jsonjson
{
"linter": {
"rules": {
"style": {
"unnecessary-string-escapes": "error"
}
}
}
}