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.

IDE0045 produced code causes compiler to flag a nullable dereference that won't happen

See original GitHub issue

Version Used: 4.3.0-3.22401.3+41ae77386c335929113f61d6f51f2663d2780443

Steps to Reproduce:

public static void TestMethod(Test? test)
    {
        if (test != null && test.Field == null)
        {
            test.Field = string.Empty;
        }
        else
        {
            throw new InvalidOperationException();
        }
    }

    public class Test
    {
        public string? Field;
    }

Expected Behavior:

No IDE0045 suggestion

Actual Behavior:

public static void TestMethod(Test? test)
    {
        // CS8602 Dereference of a possibly null reference
        test.Field = test != null && test.Field == null ? string.Empty : throw new InvalidOperationException();
    }

    public class Test
    {
        public string? Field;
    }

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:33 (32 by maintainers)

github_iconTop GitHub Comments

1reaction
AlekseyTscommented, Aug 30, 2022

@MadsTorgersen

We should note this, live with it and move on.

This sounds good to me.

it isn’t clear that the compiler’s behavior is consistent

I am not sure I agree with that. I think the behavior is consistent. If the target of an assignment is a field reference, its receiver is evaluated before the right hand side is evaluated, but is not dereferenced until we execute the store command. It is the store command that actually dereferences the receiver, we are not doing that explicitly.

The current implementation is a bug

In my opinion, this is a small deviation from the spec, rather than a bug. It is really not obvious what does it mean to evaluate a field reference when it is a target of an assignment. I also think that it should be relatively easy to adjust the spec to clarify the behavior - receivers of the target are dereferenced in the process of the store operation. This affects properties and probably array elements too.

1reaction
MaceWinducommented, Aug 30, 2022

@AlekseyTs sure. I just wanted to report incorrect behavior, didn’t expected it to escalate to this level 😄

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why do we get possible dereference null reference ...
To the compiler. As humans, we can look at this code and obviously understand that it cannot throw a null reference exception.
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 >
Nullable reference types
The compiler doesn't issue warnings when code assigns a maybe-null expression to a variable that may be null.
Read more >
Invalid ThenInclude() Nullable Reference Type Warning
Nullable reference type warnings are compiler generated. What the warning say is true that you are potentially dereferencing a null.
Read more >
[Proposal] Introduce "var?" for nullable type inference #3591
If the method returns a non-nullable, the compiler will consider the variable to not be null and will not warn on dereferencing.
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