Checking if not-nullable reference is null should produce a warning #4216
Replies: 4 comments 28 replies
-
The nullability analysis actually took the opposite route. public void M(string s) {
Console.WriteLine(s.Length); // Fine
if (s == null)
{
Console.WriteLine("Null");
}
Console.WriteLine(s.Length); // Warning
} The nullability analysis learns from your |
Beta Was this translation helpful? Give feedback.
-
This should be covered by CA1508. |
Beta Was this translation helpful? Give feedback.
-
I again ran into a bug because of this: in ASP.NET, |
Beta Was this translation helpful? Give feedback.
-
For those who are interested: https://www.nuget.org/packages/TA.Analyzers.InvalidNullChecks/ |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Consider the following:
I think there should be warnings for these situations, either "unrechable code" or "invalid comparison" - similarly to when comparing a value type to null.
Warning CS0472 The result of the expression is always 'false' since a value of type 'int' is never equal to 'null' of type 'int?'
Warning CS0162 Unreachable code detected
Granted these exact warnings shouldn't be used, because at runtime it's possible that b becomes null, however I do think two new warnings are called for.
Beta Was this translation helpful? Give feedback.
All reactions