Skip to content

xUnit2002 flags a literal int that's being cast to a reference type as not nullable #2395

@ericstj

Description

@ericstj

Build the following project which does this:

Assert.NotNull((JsonValue?)(int?)42);

The cast here should treat that int as a nullable reference type JsonValue. It's possible for a badly implemented conversion operator to return null here, which is what we're testing. The analyzer thinks we're checking the integer for null.

C:\scratch\xunitAssert\TestClass.cs(13,13): warning xUnit2002: Do not use Assert.NotNull() on value type 'int'. Remove this assert. [C:\scratch\xunitAssert\xunitAssert.csproj]

If you examine the IL of this method you can see it is indeed calling the operator and passing the result into Assert.Equals(object), so there seems to be a bug in how the analyzer identifies the type passed in. It could be related to implicit casting that happens. To get from int -> JsonValue the compiler discovers the implicit cast to JsonNode. I've recreated an isolated repro here that doesn't use the Json types:
xunitAssert.zip

Metadata

Metadata

Assignees

No one assigned

    Labels

    Analyzershttps://github.com/xunit/xunit.analyzersBugA fault in an existing featurehelp wantedA community-provided PR would be welcomed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions