question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Missing nullability warnings for [MemberNotNullWhen] parameters

See original GitHub issue

Version 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:closed
  • Created 3 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
jcouvcommented, Oct 22, 2020

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. But return M(); cannot.

1reaction
jcouvcommented, Oct 23, 2020

This looks like a brilliant solution, thanks a lot for fixing this!

I agree. Kudos to @RikkiGibson for coming up for that design 😃

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found