Skip to content

analyzer complains even when checking for null values #44463

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
MohiuddinM opened this issue Dec 13, 2020 · 5 comments
Closed

analyzer complains even when checking for null values #44463

MohiuddinM opened this issue Dec 13, 2020 · 5 comments
Labels
legacy-area-analyzer Use area-devexp instead.

Comments

@MohiuddinM
Copy link

I get an error even when checking for null value:
An expression whose value can be 'null' must be null-checked before it can be dereferenced at

class T {
  final String? s;

  T(this.s);

  void f() {
    if (s != null && s.length > 3) {}
  }
}
@lrhn
Copy link
Member

lrhn commented Dec 13, 2020

The reason that a test for null can sometimes cause a later use of a nullable variable to be allowed, is that it causes a type promotion from the nullable type to the non-nullable (sub-)type. This is the same as any other type promotion, say having a num and checking that it's an int.

Dart only performs type promotion when it's guaranteed to be safe, when the compiler is absolutely certain that the value you check is also the value you use later.
That currently means that you can only promote local variables. Even a private field might be overridden by a getter in a subclass, so the compiler cannot locally determine for sure that the value won't change between check and use.

You can copy the variable to a local variable:

void f() {
  var s = this.s;
  if (s != null && s.length > 3) { /*...*/ }
}

or you can add a null check to the length access:

void f() {
  if (s != null && s!.length > 3) { /*...*/ }
}

@lrhn
Copy link
Member

lrhn commented Dec 16, 2020

Could the analyzer give a different error message when something looks like the user wanted it to promote, but it doesn't for non-local variables.

@lrhn lrhn added the legacy-area-analyzer Use area-devexp instead. label Dec 16, 2020
@eernstg
Copy link
Member

eernstg commented Dec 16, 2020

@stereotype441, would it be overkill to let the analyzer maintain the same information about non-local variables (that is, instance and static variables of the enclosing class and top-level variables) as the information which is currently maintained for local variables, and then check for each failing member lookup whether the member is statically known based on the would-be promoted type? We could then report that the failure could be caused by a lack of promotion.

@stereotype441
Copy link
Member

@eernstg not at all. We have been talking about similar ideas for a while, just haven't gotten time to work on it. Main tracking issue is here: #38773

@srawlins
Copy link
Member

srawlins commented Feb 1, 2021

Follow #38773 for the resolution to this issue.

@srawlins srawlins closed this as completed Feb 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
legacy-area-analyzer Use area-devexp instead.
Projects
None yet
Development

No branches or pull requests

5 participants