Skip to content

Warn if a condition is always true or false #25592

@alrz

Description

@alrz

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

Metadata

Metadata

Assignees

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions