Skip to content

Introduce rule that prefers 'is (not) null' over (!)HasValue on nullable value type #818

@drieseng

Description

@drieseng

It would be great to have a rule that reports a diagnostic when HasValue is invoked on a nullable value type.

This is of course a style preference, and has no impact whatsoever on performance.
I suppose you do not want to enable this rule by default.

One could say that a "reverse" rule may be interesting as well for those that prefer HasValue over a null check.

Examples:

int? value = null;

// non-compliant
if (value.HasValue)
{
    ...
}

// compliant
if (value is not null)
{
    ...
}

// non-compliant
if (!value.HasValue)
{
    ...
}

// compliant
if (value is null)
{
    ...
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions