-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
Non-nullable value detected as nullable #58211
Comments
This is unfortunately by design. Similar issue: #36927 You can re-write the code as: if (object1 is not null)
MethodAcceptingNotNull(object1);
else if (object2 is not null)
MethodAcceptingNotNull(object2); or use null-forgiving operator |
Youssef is correct, this is by-design. Flow analysis keeps track of a state for each variable in the different branches of the code. When we get inside the |
@jcouv As I understand, this happens because in my case both variables WAS NOT marked by flow analyzer as not-null. Am I right? |
@NiJeTi Kind of, but marking both as not-null is incorrect too. If both are marked not-null then: if (object1 is not null || object2 is not null)
MethodAcceptingNotNull(object1); // since both are not-null, this won't be an error while it should. The request here, per my understanding, is a different type of flow analysis as stated in #36927 (comment). |
@NiJeTi Sorry if this is too much details. The analysis of Because states just track whenever each local can be null, there is no way to represent "object2 is not-null when object1 is maybe-null". |
@jcouv thanks for the explanation. All works as I understood. I think issue can be closed for the moment. Thank you very much! |
Version Used:
Version: 6.0.0
Commit: 4822e3c3aa
Steps to Reproduce:
Expected Behavior:
No warnings in build output
Actual Behavior:
Warning CS8604 : Possible null reference argument for parameter 'obj' in 'void MethodAcceptingNotNull(object obj)'.
The text was updated successfully, but these errors were encountered: