-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Open
Labels
Milestone
Description
While working on nullability analysis of extension properties in object initializers, I noticed that we use the visit results from arguments to infer the initial state of the created struct (see VisitObjectCreationExpressionBase usage of inferInitialObjectState).
But the visit results are pre-conversion. Some conversions can change the nullable state of an expression. I think we should be using the post-conversion visit results. Those are made available conveniently as part of PR #81871
[Fact]
public void TODO2()
{
var src = """
#nullable enable
new System.ValueTuple<C?, object>(new D(), new object()).Item1.ToString(); // missing
new System.ValueTuple<C?, object>(null, new object()).Item1.ToString(); // 1
class D { }
class C
{
public static implicit operator C?(D d) => null;
}
""";
CreateCompilation(src).VerifyEmitDiagnostics(
// (5,1): warning CS8602: Dereference of a possibly null reference.
// new System.ValueTuple<C?, object>(null, new object()).Item1.ToString(); // 1
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "new System.ValueTuple<C?, object>(null, new object()).Item1").WithLocation(5, 1));
}