Version Used: NullableReferenceTypes branch,
Steps to Reproduce:
The following program produces a Possible dereference of a null reference warning on o.M();.
class C {
void M() {
object? o = null;
if (o == null)
o.M();
}
}
you can change the code like this: o?.M() to address the issue. But a more useful warning is to say:
o == null is always true
so there should be another bug before we even reach the possible dereference.
There is a similar feature request for this analysis on the IDE side, but I think it is tightly coupled with the one that is planned for nullable reference types and they can actually exchange information to give the most relevant warning.
This could be not limited to the null constant, as any value that can be statically decided for o at that point, may produce a similar warning.
Relates to: #22743
Version Used:
NullableReferenceTypesbranch,Steps to Reproduce:
The following program produces a
Possible dereference of a null referencewarning ono.M();.you can change the code like this:
o?.M()to address the issue. But a more useful warning is to say:o == nullis always trueso there should be another bug before we even reach the possible dereference.
There is a similar feature request for this analysis on the IDE side, but I think it is tightly coupled with the one that is planned for nullable reference types and they can actually exchange information to give the most relevant warning.
This could be not limited to the
nullconstant, as any value that can be statically decided foroat that point, may produce a similar warning.Relates to: #22743