Missing nullability warnings for [MemberNotNullWhen] parameters
See original GitHub issueVersion Used:
Branch master (26 Sep 2020)
Latest commit 3e38d76 by CyrusNajmabadi:
Merge pull request #48002 from CyrusNajmabadi/implicitCreation
Use implicit object creation in the IDE layers.
Steps to Reproduce:
Compile and run the following code:
using System.Diagnostics.CodeAnalysis;
#nullable enable
class C
{
public static void Main()
{
if (tryGetValue1(false, true, out var s1))
s1.ToString(); // crash
}
static bool tryGetValue1(bool a, bool b, [MaybeNullWhen(false)] out string s1)
{
s1 = a ? "abc" : null;
return b; // no warnings
}
}
Expected Behavior:
Warning for return b;
because s1
might be initialized to a nullable value
Actual Behavior:
No warnings. The program crashes at runtime with a NullReferenceException
Notes
Looks like Roslyn just ignores any returns of boolean variables but tries to verify e.g. returns of negated variables.
In the following code snippet all methods should have warnings but only the ones that return !a
have them
using System.Diagnostics.CodeAnalysis;
#nullable enable
class C
{
static bool NullWhenFalseA(bool a, [MaybeNullWhen(false)] out string s1)
{
s1 = null;
return a; // no warnings
}
static bool NullWhenFalseNotA(bool a, [MaybeNullWhen(false)] out string s1)
{
s1 = null;
return !a; // warning
}
static bool NullWhenTrueA(bool a, [MaybeNullWhen(true)] out string s1)
{
s1 = null;
return a; // no warnings
}
static bool NullWhenTrueNotA(bool a, [MaybeNullWhen(true)] out string s1)
{
s1 = null;
return !a; // warning
}
}
Issue Analytics
- State:
- Created 3 years ago
- Comments:8 (4 by maintainers)
Top Results From Across the Web
C# static nullable check membernotnullwhen still shows ...
1 Answer 1 · I would say because GetPreCtScanObj can be called without calling the IsSuccess function, and i would like to avoid...
Read more >Resolve nullable warnings
Several compiler warnings indicate code that isn't null-safe. Learn how to address those warnings by making your code more resilient.
Read more >Start dealing with Nullable Reference Types! - Xebia
The NotNullWhen attribute can be used to tell the compiler that the given argument, which is nullable, will be not null when the...
Read more >Nullable reference types in C# 8.0
Nullable reference types are a new feature in C# 8.0. They allow you to spot places where you're unintentionally dereferencing a null value ......
Read more >Help the compiler, and the compiler will help you. ...
Here, the compiler will issue a warning: Parameter 'validOrDefaultName' must have a non-null value when exiting with 'true'.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
From discussion with LDM, we found a design that enables a few more useful warnings without introducing particularily annoying ones. We can report warnings on returns constants, but also on expressions where the analysis state is conditional and informs those particular parameters (or members). For instance,
return parameter == null;
can be analyzed. Butreturn M();
cannot.I agree. Kudos to @RikkiGibson for coming up for that design 😃