IDE0045 produced code causes compiler to flag a nullable dereference that won't happen
See original GitHub issueVersion 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:
- Created a year ago
- Comments:33 (32 by maintainers)
Top 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 >
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
@MadsTorgersen
This sounds good to me.
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.
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.
@AlekseyTs sure. I just wanted to report incorrect behavior, didn’t expected it to escalate to this level 😄