Rules / Style / unnecessary-brace-in-string-interps

unnecessary-brace-in-string-interps

lint/style/unnecessary-brace-in-string-interps
recommended
Same as unnecessary_brace_in_string_interps · Dart lints

Flags ${x} string interpolations whose braces can be dropped.

When the interpolated expression is a single plain identifier and the character immediately after the closing } cannot extend that identifier, ${name} is equivalent to the leaner $name. The braces are still required for anything more than a bare identifier (field access, calls, operators) or when the following character would otherwise merge into the name, so those cases are left alone. Raw strings and escaped \$ sequences are skipped, and a $ right after the } is treated as extending the name so the braces are kept.

Invalid

example.dartdart
// Bad: `${name}` where `name` is a simple identifier and the braces are not needed.
void examples(int count, String name) {
  final a = 'total: ${count}';
  final b = 'hi ${name}!';
  final c = '${count} items';
  final d = 'a ${name}.b';
  final e = "value=${count}";
  final f = 'label ${name}';
  print([a, b, c, d, e, f]);
}
Unnecessary braces in string interpolation; use `$name` instead.
2void examples(int count, String name) {
3 final a = 'total: ${count}';
Unnecessary braces in string interpolation; use `$name` instead.
3 final a = 'total: ${count}';
4 final b = 'hi ${name}!';
Unnecessary braces in string interpolation; use `$name` instead.
4 final b = 'hi ${name}!';
5 final c = '${count} items';
Unnecessary braces in string interpolation; use `$name` instead.
5 final c = '${count} items';
6 final d = 'a ${name}.b';
Unnecessary braces in string interpolation; use `$name` instead.
6 final d = 'a ${name}.b';
7 final e = "value=${count}";
Unnecessary braces in string interpolation; use `$name` instead.
7 final e = "value=${count}";
8 final f = 'label ${name}';

Valid

example.dartdart
// Good: braces are needed, or there are none to remove.
void examples(int count, String name, Map<String, int> m) {
  final a = 'total: $count'; // no braces
  final b = 'items: ${count}x'; // next char extends the identifier
  final c = 'sum: ${count + 1}'; // expression, not a simple identifier
  final d = 'field: ${m['a']}'; // index expression
  final e = 'call: ${name.length}'; // member access
  final f = 'literal text'; // no interpolation
  final g = '${count}9'; // next char is a digit — would extend
  final h = r'${notInterpolated}'; // raw string — not an interpolation
  print([a, b, c, d, e, f, g, h]);
}

How to configure

Set the severity of unnecessary-brace-in-string-interps in your falcon.json:

falcon.jsonjson
{
  "linter": {
    "rules": {
      "style": {
        "unnecessary-brace-in-string-interps": "error"
      }
    }
  }
}