Skip to content

cascade_invocations false positives #58050

Closed
@vsevolod19860507

Description

@vsevolod19860507

cascade_invocations false positives

void main() {
  final List<int> items = [];
  var g = 7;
  items.clear();
  items.addAll([g]); // LINT
}

Activity

added
type-bugIncorrect behavior (everything from a crash to more subtle misbehavior)
on Nov 8, 2019
changed the title [-]cascade_invocations[/-] [+]`cascade_invocations` false positives[/+] on Nov 8, 2019
jamesderlin

jamesderlin commented on Aug 13, 2020

@jamesderlin
Contributor

Why is this a false positive? What's wrong with:

items
  ..clear()
  ..addAll([g]);

?

vsevolod19860507

vsevolod19860507 commented on Aug 14, 2020

@vsevolod19860507
Author

Why is this a false positive? What's wrong with:

items
  ..clear()
  ..addAll([g]);

?

The problem is with void!

bwilkerson

bwilkerson commented on Aug 14, 2020

@bwilkerson
Member

Could you be a little more explicit? I don't see any particular problem caused by void in this example.

jamesderlin

jamesderlin commented on Aug 18, 2020

@jamesderlin
Contributor

@vr19860507 If you believe there would be some problem from clear() returning void, the point of using the cascade operator (items..clear()) instead of normal member access (items.clear()) is that items..clear() evaluates to items and ignores the return value of clear().

vsevolod19860507

vsevolod19860507 commented on Aug 19, 2020

@vsevolod19860507
Author

@jamesderlin @bwilkerson

Just enable "cascade_invocations". And just paste this code

void main() {
  final List<int> items = [];
  var g = 7;
  items.clear();
  items.addAll([g]); // LINT
}

into the editor.

And you will understand what I'm talking about!

jamesderlin

jamesderlin commented on Aug 19, 2020

@jamesderlin
Contributor

@vr19860507 The lint is telling you that you can use the cascade operator instead, as I showed in #58050. The lint seems correct; what makes you think that it is a false positive?

bwilkerson

bwilkerson commented on Aug 19, 2020

@bwilkerson
Member

Try running the following code:

void main() {
  final List<int> items = [];
  var g = 7;

  items.clear();
  items.addAll([g]);
  print(items);

  items..clear()..addAll([g]);
  print(items);
}

This will print out two identical lines because the two ways of writing the code are equivalent. In both cases any exiting contents of the list are removed (the invocation of clear) and then a single item is added to the list (the invocation of addAll).

I suspect that what wasn't clear is that the cascade operator means to invoke both clear and addAll on the result of the expression before the first cascade operator, which in this case is items.

vsevolod19860507

vsevolod19860507 commented on Aug 19, 2020

@vsevolod19860507
Author

@bwilkerson @jamesderlin Checked it now, it works!

items
  ..clear()
  ..addAll([g]);

But I remember exactly that when I opened this issue it did not work... I don't know, maybe I was mistaken, or maybe they fixed it...

Thank you anyway!

bwilkerson

bwilkerson commented on Aug 19, 2020

@bwilkerson
Member

You're welcome. I'm glad it's working for you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    devexp-linterIssues with the analyzer's support for the linter packagelegacy-area-analyzerUse area-devexp instead.linter-false-positivetype-bugIncorrect behavior (everything from a crash to more subtle misbehavior)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @pq@devoncarew@bwilkerson@jamesderlin@vsevolod19860507

        Issue actions

          `cascade_invocations` false positives · Issue #58050 · dart-lang/sdk