-
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
Inconsistency when using multiple NotNullIfNotNull attributes #57672
Comments
Thanks for reporting this issue. It is correct that Because the compiler performs a path-independent flow analysis, a check like This is discussed in more detail in #36927 (comment). |
Linking to #43906 for "and" semantics. |
Thanks for the clarification and link to background @RikkiGibson It would have made for some really pretty code, so I hope this will be revisited if/when path-dependent flow analysis is supported by the compiler. I don't see how I could solve this with public static void TestMethod(string? first, string? second)
{
string result;
if (first is not null || second is not null)
{
// this won't help
Debug.Assert(first is not null || second is not null);
// neither would this
if (first is null) {
Debug.Assert(second is not null);
}
else if (second is null) {
Debug.Assert(first is not null);
}
result = ReturnFirstOrSecondOrNullIfBothNull(first, second); // still warning
Console.WriteLine(result); // "first"
}
} instead I need to do something similar to the following, which is the pattern that I've resorted to use in my real code public static void TestMethod(string? first, string? second)
{
string result;
if (first is not null || second is not null)
{
// no warning
result = ReturnFirstOrSecondOrNullIfBothNull(first, second) ??
throw new ArgumentException("this would never throw since one of the arguments is certain to be not null");
Console.WriteLine(result); // "first"
}
} |
I'm currently migrating to .NET 6 and found some inconsistency in the use of multiple NotNullIfNotNull attribute on a method. I've tried to read all available sources online to understand if it's a bug or by design, but resort to posting here for clarification.
Version Used: main (2021-11-09)
Steps to Reproduce:
See this link for full repro
Expected Behavior:
No warnings if null values are eliminated through conditions.
Actual Behavior:
I get warnings, the lines marked with "** WARNING" are not expected.
The text was updated successfully, but these errors were encountered: