-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Lint request - avoid_cast_from_null #58822
Comments
FWIW we have a Hint for code like |
I could implement code for |
I'm guessing that isn't part of flow analysis. The question is: why not? Kind of seems like it would be useful in several ways. Are there false positives that we should be aware of? |
When you say, "that", @bwilkerson , do you mean the original ask? Like why doesn't flow analysis / type analysis determine that the type of |
I'd like to suggest that we stop encoding the "severity" level (avoid, recommend, prefer, etc.) as part of the lint name. That makes it a bit odd if we later choose to reclassify a lint (e.g. move it from recommended to core). Couldn't we just name something like this |
I believe we moved away from including severity level in names. |
I was asking about all of the ideas in this thread related to
Yes. We made that decision years ago and have been trying to enforce that in all of the newest lints. We generally name diagnostics in a way that indicates what the problem is, so |
I think some questions are getting confused here. Let me try to clarify with my understanding of the current state of things:
|
Sorry, not |
Ah, ok. The reason for that is because with expressions of the form Another option would be to go ahead and let |
I'm seeing cases internally like void main() {
[].firstWhere((e) => e==0, orElse: () => null as Never);
print('yay');
} Here, the closure is guaranteed to throw, but changing flow analysis to mark code which follows the cast as unreachable would not flag this program. I'll land the Hint which flags the cast. |
Making this a hint SGTM. This should have zero false positives. |
In mockito the
any
andanyNamed
utilities allow flexible targeting for stub behavior and required being able to be passed to any argument position. Before null safety this was accomplished by giving them the value ofnull
which could be passed to any argument type. With null safety, non-nullable arguments can't use this technique. With codegen, theMock*
subclass widens all argument types to be nullable. This requires that the type of an expression in awhen
argument needs to be typed as theMock*
class instead of the mocked type so at the position of the call passingany
the argument is statically nullable.This isn't always obvious, and a potential mistake during a migration is to try to cast the
any
to another type likewhen(someMock.someCall(any as int))
. This cast is guaranteed to fail when running with sound null safety.We should consider a lint which warns when an expression with type
Null
is cast to any non-nullable type, since that cast will always fail when running when sound null safety.The text was updated successfully, but these errors were encountered: