avoid-single-cascade-in-expression-statements

Same as avoid_single_cascade_in_expression_statements in Dart lints

Group
Style
Path
lint/style/avoid-single-cascade-in-expression-statements
Default
On (recommended)
Origin
Dart lints
Rule
140 of 512
recommended

Flags an expression statement that is a cascade with a single section.

A cascade (..) exists to chain several operations on one receiver; with a single section it buys nothing over an ordinary member access and only obscures intent. Rewrite foo..bar() as foo.bar(). Cascades with two or more sections are the idiomatic form and are not flagged.

Invalid

example.dartdart
void f1(StringBuffer a) {
  a..write('x');
}

void f2(List<int> list) {
  list..add(1);
}

void f3(Foo b) {
  b..field = 1;
}

void f4(List<int> c) {
  c..[0] = 2;
}

void f5(Foo d) {
  d..method();
}

void f6(StringBuffer e) {
  e..clear();
}

// Closure argument of a constructor invocation (`Expr::New`).
Widget f7(StringBuffer a) {
  return new ElevatedButton(onPressed: () {
    a..write('x');
  });
}

// Closure nested inside a collection literal.
List<VoidCallback> f8(StringBuffer a) {
  return [
    () {
      a..clear();
    },
  ];
}
Use `.` instead of `..` for this single cascade in an expression statement.
1void f1(StringBuffer a) {
2 a..write('x');
Use `.` instead of `..` for this single cascade in an expression statement.
5void f2(List<int> list) {
6 list..add(1);
Use `.` instead of `..` for this single cascade in an expression statement.
9void f3(Foo b) {
10 b..field = 1;
Use `.` instead of `..` for this single cascade in an expression statement.
13void f4(List<int> c) {
14 c..[0] = 2;
Use `.` instead of `..` for this single cascade in an expression statement.
17void f5(Foo d) {
18 d..method();
Use `.` instead of `..` for this single cascade in an expression statement.
21void f6(StringBuffer e) {
22 e..clear();
Use `.` instead of `..` for this single cascade in an expression statement.
27 return new ElevatedButton(onPressed: () {
28 a..write('x');
Use `.` instead of `..` for this single cascade in an expression statement.
35 () {
36 a..clear();

Valid

example.dartdart
void g1(StringBuffer a) {
  a
    ..write('x')
    ..write('y');
}

void g2(List<int> list) {
  list.add(1);
}

void g3(Foo b) {
  final x = b..field = 1;
  print(x);
}

Foo g4(Foo d) {
  return d..method();
}

void g5(StringBuffer e) {
  e
    ..clear()
    ..write('z');
}

void g6(Foo f) {
  f.method();
}

Configure avoid-single-cascade-in-expression-statements

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

falcon.jsonjson
{
  "linter": {
    "rules": {
      "style": {
        "avoid-single-cascade-in-expression-statements": "error"
      }
    }
  }
}

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

example.dartdart
// falcon-ignore lint/style/avoid-single-cascade-in-expression-statements: <reason>