unnecessary-string-escapes

Same as unnecessary_string_escapes in Dart lints

Group
Style
Path
lint/style/unnecessary-string-escapes
Default
On (recommended)
Origin
Dart lints
Rule
473 of 512
recommended

Flags backslashes that do not affect a string's value.

Raw strings are exempt. In triple-quoted strings, escaping the delimiter quote is only necessary in runs of three quotes or at the closing delimiter.

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]);
  print('''one\'quote''');
  print("""one\"quote""");
  print('''opposite\"quote''');
  print('''unknown\q''');
  print('''$a\'quote''');
  print('''\'$a''');
  print('''\' 'end''');
}
Remove the unnecessary escape backslash.
2void examples() {
3 final a = "\'";
Remove the unnecessary escape backslash.
3 final a = "\'";
4 final b = '\"';
Remove the unnecessary escape backslash.
4 final b = '\"';
5 final c = '\a';
Remove the unnecessary escape backslash.
5 final c = '\a';
6 final d = 'x\dy';
Remove the unnecessary escape backslash.
6 final d = 'x\dy';
7 final e = "50\%";
Remove the unnecessary escape backslash.
7 final e = "50\%";
8 final f = '\-';
Remove the unnecessary escape backslash.
9 print([a, b, c, d, e, f]);
10 print('''one\'quote''');
Remove the unnecessary escape backslash.
10 print('''one\'quote''');
11 print("""one\"quote""");
Remove the unnecessary escape backslash.
11 print("""one\"quote""");
12 print('''opposite\"quote''');
Remove the unnecessary escape backslash.
12 print('''opposite\"quote''');
13 print('''unknown\q''');
Remove the unnecessary escape backslash.
13 print('''unknown\q''');
14 print('''$a\'quote''');
Remove the unnecessary escape backslash.
14 print('''$a\'quote''');
15 print('''\'$a''');
Remove the unnecessary escape backslash.
15 print('''\'$a''');
16 print('''\' 'end''');

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]);
  print('''three\'\'\'quotes''');
  print('''two''\'quotes''');
  print('''closing\'''');
  print("""closing\"""");
  print('''escaped\\backslash\nnewline''');
  print(r'''raw\q\'quotes''');
  print('''${"nested"}''');
}

Configure unnecessary-string-escapes

Set its severity in falcon.json. See the configuration guide.

falcon.jsonjson
{
  "linter": {
    "rules": {
      "style": {
        "unnecessary-string-escapes": "error"
      }
    }
  }
}

Or silence one occurrence with a suppression comment; the reason is required.

example.dartdart
// falcon-ignore lint/style/unnecessary-string-escapes: <reason>
  • unnecessary-string-interpolationsunnecessary_string_interpolationsrecommended

    Flags a string literal whose entire content is a single interpolation of a provably non-nullable String.

  • unnecessary-thisunnecessary_thisrecommended

    Flags a redundant this. qualifier on member access.

  • unreachable-from-mainunreachable_from_main

    Report public declarations that no entry point of an executable library reaches.

  • use-colored-boxuse_colored_boxrecommendedflutter

    Prefers ColoredBox over a Container that only paints a color behind its child.

  • use-decorated-boxuse_decorated_boxrecommendedflutter

    Prefers DecoratedBox over a Container that only decorates its child.

  • use-design-system-itemuse-design-system-itemflutter

    Flags construction of a widget or type that should be replaced by its design-system equivalent.

  • use-enumsuse_enums

    Flags classes that emulate an enum and could be declared as one.

  • use-existing-destructuring

    Reuse or extend an existing record destructuring instead of rereading a field.