prefer-adjacent-string-concatenation

Same as prefer_adjacent_string_concatenation in Dart lints

Group
Style
Path
lint/style/prefer-adjacent-string-concatenation
Default
On (recommended)
Origin
Dart lints
Rule
312 of 512
recommended

Flags two string literals joined with the + operator.

Dart concatenates adjacent string literals at compile time, so 'a' 'b' is equivalent to 'a' + 'b' but without the runtime operator or the visual noise. The rule fires only when both operands of an Add expression are string literals; a literal added to any other expression is left alone, since adjacency cannot express that. Prefer writing the pieces side by side, which also reads naturally when splitting a long string across lines.

Invalid

example.dartdart
// Bad: joining two string literals with `+` instead of writing them adjacent.
void examples() {
  final a = 'foo' + 'bar';
  final b = 'hello, ' + 'world';
  final c = '' + 'x';
  final d = 'line1\n' + 'line2';
  final e = "a" + "b";
  final f = 'value: ' + '${1}';
  print([a, b, c, d, e, f]);
}
Use adjacent string literals instead of the `+` operator.
2void examples() {
3 final a = 'foo' + 'bar';
Use adjacent string literals instead of the `+` operator.
3 final a = 'foo' + 'bar';
4 final b = 'hello, ' + 'world';
Use adjacent string literals instead of the `+` operator.
4 final b = 'hello, ' + 'world';
5 final c = '' + 'x';
Use adjacent string literals instead of the `+` operator.
5 final c = '' + 'x';
6 final d = 'line1\n' + 'line2';
Use adjacent string literals instead of the `+` operator.
6 final d = 'line1\n' + 'line2';
7 final e = "a" + "b";
Use adjacent string literals instead of the `+` operator.
7 final e = "a" + "b";
8 final f = 'value: ' + '${1}';

Valid

example.dartdart
// Good: no `+` join of two string literals.
void examples() {
  const name = 'world';
  final a = 'foo' 'bar'; // adjacent string literals
  final b = 'hello ' + name; // right operand is not a literal
  final c = name + 'x'; // left operand is not a literal
  final d = 1 + 2; // not strings at all
  final e = 'count: ' + 3.toString(); // right operand is not a literal
  final f = ['a', 'b'].join(); // no `+` operator
  print([a, b, c, d, e, f]);
}

Configure prefer-adjacent-string-concatenation

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

falcon.jsonjson
{
  "linter": {
    "rules": {
      "style": {
        "prefer-adjacent-string-concatenation": "error"
      }
    }
  }
}

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

example.dartdart
// falcon-ignore lint/style/prefer-adjacent-string-concatenation: <reason>