Edit on GitHub

xUnit2002 Warning

Do not use null check on value type

Cause

A violation of this rule occurs when Assert.Null or Assert.NotNull are used on a value type.

Reason for rule

Value types cannot be null. As such, it does not make sense to compare them to null.

How to fix violations

To fix a violation of this rule, either remove the assertion or change the object’s type to a reference type.

Examples

Violates

[Fact]
public void ExampleTest()
{
    int result = GetSomeValue();

    Assert.NotNull(result);
    Assert.True(result > 4);
}

Does not violate

[Fact]
public void ExampleTest()
{
    int result = GetSomeValue();

    Assert.True(result > 4);
}

How to suppress violations

#pragma warning disable xUnit2002 // Do not use null check on value type
#pragma warning restore xUnit2002 // Do not use null check on value type
Copyright © 2017 .NET Foundation. Contributions welcomed at https://github.com/xunit/xunit.analyzers.