Skip to content

Arg.Is<T> forces Predicate<T?> even for non-nullable T causing CS8602 and CS8604 at every call #973

Closed

Description

@AHofstad

Since the nullable annotations work in #856, Arg.Is has this signature:
public static ref T Is<T>(Expression<Predicate<T?>> predicate)

Because the predicate parameter is unconditionally T?, the compiler treats the lambda parameter as possibly-null even when T is a non-nullable reference type. In practice this means:
substitute.DoSomething(Arg.Is<Foo>(foo => foo.Bar == "baz"));

now produces CS8602 on foo.Bar, even though the method under test (DoSomething(Foo foo)) never accepts null for that parameter. The same shows up as CS8604 when the matched value is passed onward to something expecting non-null T.

I can understand the need for null-matching, Arg.Is has to support matching literal null for reference types:
Arg.Is<string>(x => x == null)

The problem is that now this one case makes every Arg.Is call nullable, instead of only the ones that actually care about null.

I would keep Arg.Is(Expression<Predicate> predicate) non-nullable by default, so that the common case has no errors and introduce a seperate item for null-matching, something like:
public static ref T IsPossibleNull<T>(Expression<Predicate<T?>> predicate)

This keeps the normal case clean and makes the null-matching explicit instead of every test project having to blanket-suppress CS8602/CS8604 after upgrading to 6.0.

Activity

  1. stogle commented on Jul 13, 2026

    @stogle

    I am also running into this issue while trying to upgrade to 6.0.0. It looks like this problem with Arg.Is was mentioned in the comments of #856 and there was talk of reverting it but that didn't happen.

  2. self-assigned this
    on Jul 13, 2026
  3. jdb0123 commented on Jul 13, 2026

    @jdb0123

    @Romfos
    I believe the fixes should be a bit wider than #975
    I created #976
    Still work in progress

  4. Wabelbit commented on Jul 30, 2026

    @Wabelbit

    I also ran into this while upgrading to v6. Our project has a very strict policy and uses WarningsAsErrors for everything nullability-related. As such, this change broke our build and with thousands of instances of this, it's unfeasibly tedious to update. If this change would actually make sense then it could be worth it, but I don't see the point in having to null-check something that is already "guaranteed" not-null at compile-time.

    In cases where it can be null, I'm already using Arg.Is<MyClass?>(...). There was no problem to be solved here, I don't understand the point of this change.
    What makes it even worse is that Arg.Is takes an Expression, and expression-trees cannot contain null-chaining, so now I'm required to use null-enforcement ! in every use of Arg.Is with a predicate (ouch!).

    For these reasons, I really hope the proposed fix here will get accepted :)

  5. Wabelbit commented on Jul 30, 2026

    @Wabelbit

    btw, a similar issue exists for CallInfo.Arg now, which always returns nullable, even when given a non-nullable T. Would you like to solve that as part of the same PR, or prefer me to describe it in a separate issue?

  6. cremor commented on Jul 30, 2026

    @cremor

    See the discussion in #976

  7. zvirja commented on Aug 8, 2026

    @zvirja
    Contributor

    We reverted nullability annotations in v6.1, so it shall no longer be an issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions