Skip to content

cascade_invocations is too eager when type promotion is necessary #57970

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

Open
ghost opened this issue May 29, 2019 · 10 comments
Open

cascade_invocations is too eager when type promotion is necessary #57970

ghost opened this issue May 29, 2019 · 10 comments
Labels
area-devexp For issues related to the analysis server, IDE support, linter, `dart fix`, and diagnostic messages. customer-flutter devexp-linter Issues with the analyzer's support for the linter package linter-false-positive P3 A lower priority bug or feature request type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)

Comments

@ghost
Copy link

ghost commented May 29, 2019

@Hixie commented on Mar 4, 2018, 10:53 AM UTC:

Enable the cascade_invocations lint, then analyze this:

class A { }
class B extends A { void foo() { } }

void test(A x) {
  if (A is B) {
    final B b = x;
    b.foo();
  }
}

void main() { }

It says: lint • Cascade consecutive method invocations on the same reference at test.dart:7:5 • cascade_invocations

However, if you change it as suggested:

class A { }
class B extends A { void foo() { } }

void test(A x) {
  if (A is B) {
    final B b = x
      ..foo();
  }
}

void main() { }

...then now the code is no longer correct (error • The method 'foo' isn't defined for the class 'A' at test.dart:7:9 • undefined_method).

This issue was moved by pq from dart-lang/sdk#32407.

@ghost
Copy link

ghost commented May 29, 2019

@lrhn commented on Mar 6, 2018, 10:40 AM UTC:

Probably don't mean to say if (A is B), but if you do if (x is B) the warning goes away.

@ghost
Copy link

ghost commented May 29, 2019

@Hixie commented on Mar 6, 2018, 7:10 PM UTC:

Oops, sorry about that typo.

I don't think it changes this bug, though. That conditional could be anything that happens to imply, in a way the analyzer can't recognize, that the cast is ok; the cascade operator isn't appropriate in this case.

@ghost
Copy link

ghost commented May 29, 2019

@bwilkerson commented on Mar 6, 2018, 7:17 PM UTC:

I'm having trouble thinking of a concrete example of the condition you described. I'm assuming that you actually ran into this somewhere. Can you point us at the actual code that caused the problem?

@ghost
Copy link

ghost commented May 29, 2019

@ghost
Copy link

ghost commented May 29, 2019

@bwilkerson commented on Mar 6, 2018, 9:01 PM UTC:

Ok, thanks. The problem arrises when the variable cannot be promoted. Sounds like the lint might be checking the wrong type at that point.

@ghost
Copy link

ghost commented May 29, 2019

@bwilkerson commented on Mar 14, 2018, 2:15 PM UTC:

@alexeieleusis

@ghost
Copy link

ghost commented May 29, 2019

@leafpetersen commented on Mar 15, 2018, 1:01 AM UTC:

The second error message is actually incorrect, see #30542

The intention is that

 final B b = x
      ..foo();

should be treated (after implicit downcast insertion) as:

 final B b = (x as B)
      ..foo();

Neither of the front ends implements this entirely yet though.

@ghost
Copy link

ghost commented May 29, 2019

@srawlins commented on May 29, 2019, 3:35 PM UTC:

CC @pq move to linter?

@pq pq added type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) customer-flutter linter-false-positive and removed P2 A bug or feature request we're likely to work on labels May 29, 2019
@Hixie
Copy link
Contributor

Hixie commented May 30, 2019

(An example of where you might run into this is where the condition is checking runtimeType instead of using is, which is common e.g. in operator == implementations; or in cases where you have some tag that determines the actual type of some variable, and the condition is checking the tag to determine what to do (e.g. because some of the other cases might be distinguishing "double" meaning "centimeters" vs "double" meaning "inches" or whatever, so in general you can't rely on the type).)

@nstrelow
Copy link

nstrelow commented Dec 7, 2020

Friendly asking, has this been fixed, or is the lint still breaking sometimes?
Thanks for the hard work!

@srawlins srawlins added the P3 A lower priority bug or feature request label Sep 22, 2022
@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
@bwilkerson bwilkerson added area-devexp For issues related to the analysis server, IDE support, linter, `dart fix`, and diagnostic messages. and removed legacy-area-analyzer Use area-devexp instead. labels Feb 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-devexp For issues related to the analysis server, IDE support, linter, `dart fix`, and diagnostic messages. customer-flutter devexp-linter Issues with the analyzer's support for the linter package linter-false-positive P3 A lower priority bug or feature request type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)
Projects
None yet
Development

No branches or pull requests

7 participants