prefer-interpolation-to-compose-strings

Same as prefer_interpolation_to_compose_strings in Dart lints

Group
Style
Path
lint/style/prefer-interpolation-to-compose-strings
Default
On (recommended)
Origin
Dart lints
Rule
375 of 512
recommended

Prefers string interpolation to composing strings with +.

Each + chain (a + b + c, flattened through unparenthesized left operands) is scanned pair by pair: a pair is reported, from its left operand to its right one, when exactly one side is a string literal (plain, interpolated, or adjacent), neither side is a single raw literal (r'...') nor a toString(...) call with arguments, and the left operand is a String. The pair after a reported one is skipped. A non-+ binary expression as a chain's left operand ends the chain (a - b + 'x' checks only 'x'), and a parenthesized operand is an ordinary operand. Each left spine is collected iteratively once; preorder visits its prefixes before any operand changes semantic flow, so the root already covers every greedy pair a prefix would report.

falcon stays silent when it cannot infer the left operand's type.

Invalid

example.dartdart
String greeting(String name, String city) {
  return 'Hello, ' + name + ' from ' + city;
}
String wrap(String value) => value;
String nested(String name) {
  return 'outer ' + wrap('inner ' + name);
}
String grouped(String name) => ('a' + name) + 'b';
String suffix(String name) => name + '!';
String afterInterpolation(String name, String tail) => 'Hello $name' + tail;
Use string interpolation to compose strings and values.
1String greeting(String name, String city) {
2 return 'Hello, ' + name + ' from ' + city;
Use string interpolation to compose strings and values.
1String greeting(String name, String city) {
2 return 'Hello, ' + name + ' from ' + city;
Use string interpolation to compose strings and values.
5String nested(String name) {
6 return 'outer ' + wrap('inner ' + name);
Use string interpolation to compose strings and values.
5String nested(String name) {
6 return 'outer ' + wrap('inner ' + name);
Use string interpolation to compose strings and values.
7}
8String grouped(String name) => ('a' + name) + 'b';
Use string interpolation to compose strings and values.
7}
8String grouped(String name) => ('a' + name) + 'b';
Use string interpolation to compose strings and values.
8String grouped(String name) => ('a' + name) + 'b';
9String suffix(String name) => name + '!';
Use string interpolation to compose strings and values.
9String suffix(String name) => name + '!';
10String afterInterpolation(String name, String tail) => 'Hello $name' + tail;

Valid

example.dartdart
String greeting(String name, String city) => 'Hello, $name from $city';
String adjacent = 'hello ' 'world';
String raw(String value) => r'raw' + value;

class CustomAdd {
  CustomAdd operator +(String value) => this;
}
CustomAdd custom(CustomAdd value) => value + 'text';

// Two literals, interpolated or not, are not a composition.
String interpolated(String name) => 'Hello $name' + '!';

class Padded {
  @override
  String toString({int pad = 0}) => '';
}
String padded(Padded value) => 'x' + value.toString(pad: 1);

Configure prefer-interpolation-to-compose-strings

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

falcon.jsonjson
{
  "linter": {
    "rules": {
      "style": {
        "prefer-interpolation-to-compose-strings": "error"
      }
    }
  }
}

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

example.dartdart
// falcon-ignore lint/style/prefer-interpolation-to-compose-strings: <reason>
  • prefer-is-emptyprefer_is_emptyrecommended

    Flags .length comparisons equivalent to a .isEmpty check.

  • prefer-is-not-emptyprefer_is_not_emptyrecommended

    Flags .length comparisons equivalent to a .isNotEmpty check.

  • prefer-is-not-operatorprefer_is_not_operatorrecommended

    Flags a negated type test !(x is T) that should use the is! operator.

  • prefer-iterable-ofprefer-iterable-ofrecommended

    Flags .from collection constructors in favor of the .of constructor.

  • prefer-iterable-where-typeprefer_iterable_whereTyperecommended

    Flags .where((e) => e is T), which .whereType<T>() expresses directly.

  • prefer-lastprefer-last

    Flags manual last-element indexing in favor of the .last getter.

  • prefer-match-file-name

    Match a file's sole class or enum name to its snake-case basename.

  • prefer-mixinprefer_mixinrecommended

    Flags with clauses that mix in a plain class instead of a mixin.