Rules / Style / prefer-adjacent-string-concatenation

prefer-adjacent-string-concatenation

lint/style/prefer-adjacent-string-concatenation
recommended
Same as prefer_adjacent_string_concatenation · Dart lints

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]);
}

How to configure

Set the severity of prefer-adjacent-string-concatenation in your falcon.json:

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