Skip to content

cascade_invocations false positives #58050

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
vsevolod19860507 opened this issue Nov 6, 2019 · 9 comments
Closed

cascade_invocations false positives #58050

vsevolod19860507 opened this issue Nov 6, 2019 · 9 comments
Labels
devexp-linter Issues with the analyzer's support for the linter package legacy-area-analyzer Use area-devexp instead. linter-false-positive type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)

Comments

@vsevolod19860507
Copy link

cascade_invocations false positives

void main() {
  final List<int> items = [];
  var g = 7;
  items.clear();
  items.addAll([g]); // LINT
}
@pq pq added type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) linter-false-positive labels Nov 8, 2019
@pq pq changed the title cascade_invocations cascade_invocations false positives Nov 8, 2019
@jamesderlin
Copy link
Contributor

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

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

?

@vsevolod19860507
Copy link
Author

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

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

?

The problem is with void!

@bwilkerson
Copy link
Member

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

@jamesderlin
Copy link
Contributor

jamesderlin commented Aug 18, 2020

@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
Copy link
Author

vsevolod19860507 commented Aug 19, 2020

@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
Copy link
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
Copy link
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
Copy link
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
Copy link
Member

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

@devoncarew devoncarew added devexp-linter Issues with the analyzer's support for the linter package legacy-area-analyzer Use area-devexp instead. labels Nov 18, 2024
@devoncarew devoncarew transferred this issue from dart-archive/linter Nov 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
devexp-linter Issues with the analyzer's support for the linter package legacy-area-analyzer Use area-devexp instead. linter-false-positive type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)
Projects
None yet
Development

No branches or pull requests

5 participants