-
Notifications
You must be signed in to change notification settings - Fork 205
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
[NNBD ] Static Analyzer incorrectly thinks casts will always succeed. #1523
Comments
Ah I see what is happening now, the cast needs to become I guess this is working as designed? Sneaky one, but it makes sense. |
The warning and your conclusion are correct. Related: |
cc @bwilkerson @stereotype441 not sure if there's anything actionable here or not, but on the topic of error messages, this is a slightly tricky one to reason out as a user. It's almost the dual of the "failed promotion" problem, in that a promotion did occur, which caused a warning. |
The topic has come up before, but with no concrete action items. I believe that the last time it came up one of the suggestions was a lint that would flag casts where the only effect is to remove nullability. While that might be an interesting lint in its own right, it seems somewhat tangential. We could take a similar approach to the failed promotion work and track where promotion happened so that we could report something indicating where the promotion that caused the error occurred. That approach has the advantage that it's symmetric. Another thought would be to attach enough information to the AST that we could display inference information in other ways, such as hover text. It might be useful if we could tell the user that Those two possibilities are not mutually exclusive. |
I like that way of thinking of it. In the same way that we're working on displaying "why not promoted" context for error messages, we could certainly add "why promoted" context for warnings like this one. So in the example: RenderBox? rb = c.findRenderObject() as RenderBox;
return rb?.size ?? Size.zero; The message would become something like:
@esDotDev Do you think that attaching this extra information to the warning message would have made it easier to figure out what was happening? I filed dart-lang/sdk#45332 to track this idea. |
Ya I think that would be very helpful. I think my confusion was two-fold
Once I understand that these are truly different types, and that the |
@esDotDev yeah, that's a tricky corner case and you're not the first person to run into it. We added an extension called The short story is that if you update your pubspec to depend on a recent enough version of the import 'package:collection/collection.dart' show IterableExtension; And then you should be able to do this: T? item = items.firstWhereOrNull((item) => item.documentId = oldItem?.documentId); Incidentally, this is one of the subtle changes that the migration tool is supposed to do automatically for you, but I completely understand if you haven't been able to use the migration tool due to dart-lang/sdk#45326. (Thank you for filing that bug, BTW. I'm working on a fix now). |
Sweet, that's exactly what I was hoping for, ty! |
Fwiw, ended up as this workaround for vanilla dart. Costs a line, but it avoids the cast warning, and reads/writes like we're used to:
|
The lint cast_nullable_to_non_nullable can help. |
I have updated the documentation for this diagnostic to include an example showing this case (https://dart-review.googlesource.com/c/sdk/+/191420). |
Consider this code:
findRenderObject
returnsRenderObject?
, so clearlyrb
could be null here, but the compiler is giving a warning:warning: The receiver can't be null, so the null-aware operator '?.' is unnecessary. (invalid_null_aware_operator at [flutter_folio] lib_utils\context_utils.dart:9)
Compiler seems to think the cast will always succeed, even if the source is a nullable.
Nullable or not, it should not be making this assumption?
The text was updated successfully, but these errors were encountered: