IsNull

IsNull

Summary

Use “… == null” or “… is {}” instead of “… is null.”

Default severity

Info

Description

This rule reports diagnostic information of using is pattern matching with the null Constant Pattern.

Note that the default diagnostic severity of this analyzer is Information.

Code fix

The code fix provides an option replacing the expressions “… is null” and “!(… is null)” with “… == null” and “… != null,” respectively. Also, it provides another option replacing them with “!(… is {})” and “… is {},” respectively.

Remarks

Replacing the expression “… is null” with “… == null,” as well as replacing “!(… is null)” with “… != null,” can be a breaking change. For more information, refer to the description of EqualsNull code fix.

Example

Diagnostic

public void Method(object? o, string? s)
{
    if (o is null && !(s is null))
    {
        ⋮
    }
    ⋮

Code fix (with the equality operators)

public void Method(object? o, string? s)
{
    if (o == null && s != null)
    {
        ⋮
    }
    ⋮

Code fix (with the property pattern)

public void Method(object? o, string? s)
{
    if (!(o is {}) && s is {})
    {
        ⋮
    }
    ⋮