no-adjacent-strings-in-list

Same as no_adjacent_strings_in_list in Dart lints

Path
lint/correctness/no-adjacent-strings-in-list
Default
On (recommended)
Origin
Dart lints
Rule
280 of 512
recommended

Flags adjacent string literals used as an element of a list or set literal.

Adjacent strings concatenate, so a missing comma between two elements silently merges them into one:

var list = <String>[
  'a'      // flagged: a comma is missing, the list has two elements
  'b',
  'c',
];
var list = <String>['a' + 'b', 'c']; // fine: concatenation is explicit

Also checked: the body of a for element, the then element of an if element without else (or the else element when present), and constant string elements of a list pattern in a switch statement case. Map literals and parenthesized elements are not checked.

Invalid

example.dartdart
void use(Object o) {}

void main(bool flag, List<String> xs) {
  use(<String>[
    'a'
    'b',
    'c',
  ]);
  use(['x' 'y']);
  use(<String>{'set' 'element'});
  use([if (flag) 'then' 'element']);
  use([if (flag) 'then' else 'else' 'element']);
  use([for (var x in xs) 'body' 'part']);
  switch (xs) {
    case ['pattern' 'element']:
      break;
  }
}

void nestedCollectionContexts(bool flag, List<String> xs) {
  use([
    for (final x in xs)
      if (flag)
        for (final y in xs)
          'nested' 'body',
  ]);
  use([
    if (flag)
      if (flag) 'innerThen' else 'inner' 'else'
    else
      for (final x in xs)
        'outer' 'else',
  ]);
  use([
    if (['condition' 'list'].isNotEmpty) 'body',
    for (final x in ['header' 'list']) x,
    ...['spread' 'list'],
  ]);
}
Don't use adjacent strings in a list literal.
4 use(<String>[
5 'a'
Don't use adjacent strings in a list literal.
8 ]);
9 use(['x' 'y']);
Don't use adjacent strings in a list literal.
9 use(['x' 'y']);
10 use(<String>{'set' 'element'});
Don't use adjacent strings in a list literal.
10 use(<String>{'set' 'element'});
11 use([if (flag) 'then' 'element']);
Don't use adjacent strings in a list literal.
11 use([if (flag) 'then' 'element']);
12 use([if (flag) 'then' else 'else' 'element']);
Don't use adjacent strings in a list literal.
12 use([if (flag) 'then' else 'else' 'element']);
13 use([for (var x in xs) 'body' 'part']);
Don't use adjacent strings in a list literal.
14 switch (xs) {
15 case ['pattern' 'element']:
Don't use adjacent strings in a list literal.
24 for (final y in xs)
25 'nested' 'body',
Don't use adjacent strings in a list literal.
28 if (flag)
29 if (flag) 'innerThen' else 'inner' 'else'
Don't use adjacent strings in a list literal.
31 for (final x in xs)
32 'outer' 'else',
Don't use adjacent strings in a list literal.
34 use([
35 if (['condition' 'list'].isNotEmpty) 'body',
Don't use adjacent strings in a list literal.
35 if (['condition' 'list'].isNotEmpty) 'body',
36 for (final x in ['header' 'list']) x,
Don't use adjacent strings in a list literal.
36 for (final x in ['header' 'list']) x,
37 ...['spread' 'list'],

Valid

example.dartdart
void use(Object o) {}

void main(bool flag, List<String> xs) {
  use(<String>['a' + 'b', 'c']);
  use(['a', 'b']);
  use([('paren' 'thesized')]);
  use({'map' 'key': 'value' 'part'});
  use([if (flag) 'then' 'quirk' else 'else']);
  use([...xs]);
  use('not' 'in a list');
  switch (xs) {
    case ['a', 'b']:
      break;
  }
}

void collectionContextsDoNotLeak(bool flag, List<String> xs) {
  use([
    if (('condition' 'text').isNotEmpty) 'body',
    for (final x in ('header' 'text').split('')) x,
    ...('spread' 'text').split(''),
    if (flag)
      'outerThen' 'excluded'
    else
      if (flag) 'innerThen' else 'innerElse',
  ]);
  use([
    if (flag) 'excluded' 'then' else 'included',
    for (final x in xs) ('parenthesized' 'body'),
  ]);
  use('outside' 'collection');
}

Configure no-adjacent-strings-in-list

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

falcon.jsonjson
{
  "linter": {
    "rules": {
      "correctness": {
        "no-adjacent-strings-in-list": "error"
      }
    }
  }
}

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

example.dartdart
// falcon-ignore lint/correctness/no-adjacent-strings-in-list: <reason>
  • no-import-cyclescross-file

    Report mutually dependent components of imported or exported library files.

  • no-logic-in-create-stateno_logic_in_create_staterecommendedflutter

    Require StatefulWidget.createState to return only a fresh zero-argument Flutter State construction.

  • no-self-assignmentsno_self_assignmentsrecommended

    Flags assignments of a variable to itself, such as x = x;.

  • null-check-on-nullable-type-parameternull_check_on_nullable_type_parameterrecommended

    Flags a null assertion x! on a nullable type parameter T? whose expected type is T or T? for a potentially nullable T, and null-assert patterns whose matched value is a nullable type parameter.

  • null-closuresnull_closuresrecommended

    Reject literal null in the closure slots of curated SDK APIs.

  • prefer-correct-json-casts

    Decode JSON containers using their actual runtime generic types.

  • prefer-define-hero-tagflutter

    Require an explicit heroTag on Flutter FloatingActionButton constructors.

  • prefer-expect-latertest

    Requires expectLater when a canonical expect call receives an unawaited Future.