Skip to content

Relax cascade_invocations lint for assignments? #57944

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
jamesderlin opened this issue Apr 22, 2019 · 3 comments
Closed

Relax cascade_invocations lint for assignments? #57944

jamesderlin opened this issue Apr 22, 2019 · 3 comments
Labels
devexp-linter Issues with the analyzer's support for the linter package legacy-area-analyzer Use area-devexp instead. linter-false-positive

Comments

@jamesderlin
Copy link
Contributor

I had some code:

_imageStream?.removeListener(_onImageUpdated);
_imageStream = newImageStream;
_imageStream.addListener(_onImageUpdated);

I have the cascade_invocations lint enabled because using the cascade operator still doesn't quite come naturally to me. The lint suggested changing it to:

_imageStream?.removeListener(_onImageUpdated);
_imageStream = newImageStream..addListener(_onImageUpdated);

However, using the cascade operator here changes the order of operations, and it led to a bug where the callback fired synchronously and failed assertions because _imageStream had not yet been set to newImageStream.

Perhaps the cascade_invocations lint should be relaxed if the expression is being used as the RHS of an assignment? (Or possibly if the cascaded expression would be the RHS of an assignment and if we can somehow determine that the expression is doing something non-trivial that could depend on the LHS of the assignment.)

(I suppose something else to consider is whether foo = bar..baz() could be treated as (foo = bar)..baz() instead of as foo = (bar..baz), but I admit that that probably would be unexpected, particularly if the cascaded expression all fits on one line.)

@pq
Copy link
Member

pq commented Apr 22, 2019

Good catch. Thanks @jamesderlin!

Perhaps the cascade_invocations lint should be relaxed if the expression is being used as the RHS of an assignment?

This sounds reasonable to me.

@bwilkerson: what do you think?

@bwilkerson
Copy link
Member

I think this is a hard question. :-)

The problem is that we can't, in general, tell whether the variable being assigned can be referenced from within the code being invoked after the assignment (except in the case where it's being assigned to a local variable). If we could, I'd recommend that we fix the lint to do the work so that the user doesn't need to.

But given that we can't, the question becomes, will enough users run into the kind of issue that @jamesderlin did that we should limit the lint, or is it a small enough percentage that we should recommend adding an //ignore comment when it does arise?

Limiting the lint would cause it to not be able to catch cases like

_list = _getDefaultList();
_list.add(a);
_list.add(b);

which seems like one of the cases the lint was designed to catch.

@srawlins
Copy link
Member

Duplicate of #57631

@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
Projects
None yet
Development

No branches or pull requests

5 participants