Rules / Style / unnecessary-string-interpolations

unnecessary-string-interpolations

lint/style/unnecessary-string-interpolations
recommended
Same as unnecessary_string_interpolations · Dart lints

Flags a string literal whose entire content is a single interpolation.

When a string is nothing but '$x' or '${x}', wrapping the value in quotes and interpolation markers is redundant if the value is already a string — the literal is equivalent to writing the expression directly. The quotes only add noise and obscure that no formatting or concatenation is happening. The rule fires only when the interpolation spans the whole content, whether a bare $identifier or a full ${...} expression; a string with any surrounding text, or two adjacent interpolations like '$a$b', is left alone. Raw strings are never flagged. Use the interpolated expression on its own.

Invalid

example.dartdart
// Bad: the whole string is a single interpolation, so the wrapping string adds nothing.
String examples(String name, Object value, int n) {
  final a = '$name';
  final b = '${name}';
  final c = '${value}';
  final d = '${name.toUpperCase()}';
  final e = "$name";
  final f = '${n + 1}';
  return '$a$b$c$d$e$f';
}
Unnecessary string interpolation; use the expression directly.
2String examples(String name, Object value, int n) {
3 final a = '$name';
Unnecessary string interpolation; use the expression directly.
3 final a = '$name';
4 final b = '${name}';
Unnecessary string interpolation; use the expression directly.
4 final b = '${name}';
5 final c = '${value}';
Unnecessary string interpolation; use the expression directly.
5 final c = '${value}';
6 final d = '${name.toUpperCase()}';
Unnecessary string interpolation; use the expression directly.
6 final d = '${name.toUpperCase()}';
7 final e = "$name";
Unnecessary string interpolation; use the expression directly.
7 final e = "$name";
8 final f = '${n + 1}';

Valid

example.dartdart
// Good: the string is more than a single bare interpolation.
String examples(String name, int n) {
  final a = 'Hello $name'; // has surrounding text
  final b = '$name!'; // trailing text
  final c = '${name}s'; // trailing text
  final d = '$name$name'; // two interpolations
  final e = 'count: ${n}'; // leading text
  final f = 'plain'; // no interpolation
  final g = r'$name'; // raw string — literal `$name`, not an interpolation
  return '$a$b$c$d$e$f$g';
}

How to configure

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

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