Skip to content
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

No warning in case of variable assignment before the first type test #59614

Open
sgrekhov opened this issue Nov 26, 2024 · 1 comment
Open

No warning in case of variable assignment before the first type test #59614

sgrekhov opened this issue Nov 26, 2024 · 1 comment
Labels
area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. P3 A lower priority bug or feature request type-question A question about expected behavior or functionality

Comments

@sgrekhov
Copy link
Contributor

sgrekhov commented Nov 26, 2024

class C {}
class D {}

main() {
  Object o = C();
  if (o is C) {} // No warning
  o = C();
  if (o is C) {} // unnecessary_type_check warning.
  o = D();
  if (o is D) {} // No warning
  if (o is D) {} // No warning
  o = D();
  if (o is D) {} // unnecessary_type_check warning.
}

Why there is no warning after the first assignment of the new object instance?

Dart SDK version: 3.7.0-183.0.dev (dev) (Fri Nov 22 20:03:46 2024 -0800) on "windows_x64"

@sgrekhov sgrekhov added the area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. label Nov 26, 2024
@sgrekhov sgrekhov changed the title No warning in case of variable assignment before type test No warning in case of variable assignment before the first type test Nov 26, 2024
@eernstg
Copy link
Member

eernstg commented Nov 26, 2024

That's OK!

The promotion mechanism relies on 'types of interest', and the initialization of o with a value of type C doesn't make C a type of interest (it only makes a non-nullable type a type of interest in cases like int? iq = 2;). The same thing is true for assignments, they only promote the type of a variable if that type is already of interest for that variable for other reasons.

This means that o is C tests whether an o of type Object also has type C, which is OK (non-redundant).

But in the code that follows the first if statement, C is a type of interest for o, so o = C() promotes o to C, which makes the second type test redundant.

The cases testing o is D are similar: o is D turns D into a type of interest for o, and the second o = D() then promotes o. The first assignment o = D() does not promote o because D is not of interest for o at that time.

@keertip keertip added type-question A question about expected behavior or functionality P3 A lower priority bug or feature request labels Nov 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. P3 A lower priority bug or feature request type-question A question about expected behavior or functionality
Projects
None yet
Development

No branches or pull requests

3 participants