Skip to content

No promotion of nullable type in "if (x?.y ?? false)" #56693

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
DanTup opened this issue Sep 10, 2024 · 3 comments
Closed

No promotion of nullable type in "if (x?.y ?? false)" #56693

DanTup opened this issue Sep 10, 2024 · 3 comments
Labels
legacy-area-analyzer Use area-devexp instead. triage-automation See https://github.com/dart-lang/ecosystem/tree/main/pkgs/sdk_triage_bot. type-enhancement A request for a change that isn't a bug

Comments

@DanTup
Copy link
Collaborator

DanTup commented Sep 10, 2024

I thought there might be an existing issue fort his, but I couldn't find one. Often I find myself writing code like:

void x(List<int>? ints) {
  // Only do the thing if the list is non-null _and_ not empty
  if (ints?.isNotEmpty ?? false) {
    // Do the thing with the not-null list
    print(ints.length);
  }
}

However this reports "The property 'length' can't be unconditionally accessed because the receiver can be 'null'". My usual fix is to have an explicit null check:

  if (ints != null && ints.isNotEmpty) {

But this feels a little verbose. Maybe it'd be nice to have a .isNullOrEmpty, but that'd have to be known to the analyzer (if I implement it as an extension member, the analyzer wouldn't know it could be promoted).

@dart-github-bot
Copy link
Collaborator

Summary: The user is experiencing an issue where the analyzer doesn't promote a nullable type to a non-nullable type after a null-aware cascade and null coalescing operator. This results in a warning when accessing a property on the potentially null object.

@dart-github-bot dart-github-bot added legacy-area-analyzer Use area-devexp instead. triage-automation See https://github.com/dart-lang/ecosystem/tree/main/pkgs/sdk_triage_bot. type-enhancement A request for a change that isn't a bug labels Sep 10, 2024
@eernstg
Copy link
Member

eernstg commented Sep 10, 2024

There is some overlap with dart-lang/language#1224. Perhaps these two issues could be combined into a request for something which is a bit more general?

@DanTup
Copy link
Collaborator Author

DanTup commented Sep 10, 2024

Thanks - looks like that issue already discusses some additional cases to the one in the title, including this one. And actually, this is an exact dupe of dart-lang/language#4006 which was closed in favour of that one.

So let's close this - it seems likely if dart-lang/language#1224 is addressed, this case would be covered because it's probably one of the simpler ones mentioned.

@DanTup DanTup closed this as completed Sep 10, 2024
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. triage-automation See https://github.com/dart-lang/ecosystem/tree/main/pkgs/sdk_triage_bot. type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

3 participants