Skip to content

Make Assert.IsNotNull generic and have it return the value that was checked #10918

Closed as not planned

Description

@drieseng

Summary

I'm 120% sure that the boat has long sailed for this proposal (with probably very good reasons, such as code side explosion), but I'll try anyway.

It would've been nice if the Assert.IsNotNull() overloads where generic and returned the value that was passed to it.
This would've allowed for nice code like this:

var fi = new System.IO.FileInfo(@"c:\temp\x.log");
var dir = Assert.IsNotNull(fi.Directory);
Assert.AreEqual("temp", dir.Name);

Activity

  1. added
    needs/triage
    on Sep 1, 2026
  2. added
    area/assertion
    on Sep 1, 2026
  3. changed the title [-]MAke Assert.IsNotNull generic and have it return the value that was checked[/-] [+]Make Assert.IsNotNull generic and have it return the value that was checked[/+] on Sep 2, 2026
  4. Youssef1313 commented on Sep 2, 2026

    @Youssef1313
    Member

    What's the point/value of making this change?

    How is that different from either:

    var fi = new System.IO.FileInfo(@"c:\temp\x.log");
    Assert.IsNotNull(fi.Directory);
    Assert.AreEqual("temp", fi.Directory.Name);

    or

    var fi = new System.IO.FileInfo(@"c:\temp\x.log");
    var dir = fi.Directory;
    Assert.IsNotNull(dir);
    Assert.AreEqual("temp", dir.Name);
  5. Evangelink commented on Sep 2, 2026

    @Evangelink
    Member

    The main benefit is that the assertion can be used as a narrowing expression without evaluating the value twice:

    var dir = Assert.IsNotNull(fi.Directory);
    Assert.AreEqual("temp", dir.Name);

    The first alternative evaluates fi.Directory again, which can matter for computed or stateful properties. The second is equivalent but requires a separate declaration and assertion. Returning the checked value also aligns with value-returning assertions such as Assert.IsInstanceOfType<T> and Assert.ContainsSingle.

    I have prototyped this by adding generic overloads while retaining the existing void IsNotNull(object?, ...) overloads as binary-compatibility shims. The repository builds successfully, and the TestFramework unit tests pass across all target frameworks.

  6. removed
    needs/triage
    on Sep 2, 2026
  7. Youssef1313 commented on Sep 2, 2026

    @Youssef1313
    Member

    Adding an additional generic overload is risky. It's also possible that it introduces a source breaking change (for F# maybe - needs to be double checked)

    If anything to be done here, I would vote for doing a breaking change in v5 to drop the non-generic overload and only have the generic one (binary breaking). But even then I'm not sure it brings enough value to take a breaking change for.

    In addition to that, I think it does not make sense for Assert.IsNull to follow the same pattern, and the API will feel weird if IsNull and IsNotNull are not consistent.

  8. Evangelink commented on Sep 2, 2026

    @Evangelink
    Member

    The benefit here is ergonomic rather than a capability gap: a temporary variable followed by Assert.IsNotNull is equally safe and evaluates the expression only once.

    I agree that it is cleaner to wait for the next breaking release and change the existing API shape, rather than permanently retain void overloads alongside generic value-returning overloads solely for binary compatibility.

    The apparent asymmetry with Assert.IsNull is consistent with existing APIs. Assert.IsInstanceOfType<T> and Assert.IsExactInstanceOfType<T> return the successfully narrowed T, while Assert.IsNotInstanceOfType<T> and Assert.IsNotExactInstanceOfType<T> return void. Positive assertions can expose a useful narrowed value; their negative counterparts cannot.

    Moving this to the MSTest 5.0 / MTP 3.0 milestone.

  9. added
    type/breaking-change
    on Nov 5, 2026
  10. drieseng commented on Nov 5, 2026

    @drieseng
    Author

    Amaury Levé (@Evangelink), thanks again!! Would it be possible for you to work part-time on the NuGet (Package Manager) team? :)

  11. Evangelink commented on Nov 6, 2026

    @Evangelink
    Member

    Amaury Levé (@Evangelink), thanks again!! Would it be possible for you to work part-time on the NuGet (Package Manager) team? :)

    Thanks for the warm feedback :) Any particular issue I could poke people about?

  12. Evangelink commented on Nov 9, 2026

    @Evangelink
    Member

    Gert Driesen (@drieseng) We tried this for MSTest 5, but downstream integration exposed a usability regression that outweighs the convenience.

    In existing C# async methods, Assert.IsNotNull(task); now produces CS4014 because the assertion returns the checked Task. With warnings treated as errors, those existing assertions stop compiling. This broke all 10 build legs in dotnet/sdk#56993; see the original failing build.

    Requiring an explicit discard for an ordinary null assertion is not the API experience we want. Adding await is not an equivalent fix: it awaits the checked operation, not a null check, and can change control flow or hang shutdown code when cancellation comes afterwards.

    We will keep the original void overloads and their [NotNull] annotations. A local variable followed by the assertion already gives single evaluation and nullable-flow narrowing without changing the assertion's contract.

    The revert of microsoft/testfx#12219 is prepared in microsoft/testfx#12273, and we won't implement the return-value change to Assert.IsNotNull.

  13. drieseng commented on Nov 9, 2026

    @drieseng
    Author

    Amaury Levé (@Evangelink), it's a pity that this won't make the cut, but it's not a total surprise. A new method would have been an option, but coming up with a proper name would be a challenge. The new API would perhaps also introduce confusion.

    If only extension members would allow one to override a member that is declared on a type :-)

    Again thanks for considering this change!

  14. Evangelink commented on Nov 10, 2026

    @Evangelink
    Member

    Thanks for the suggestion, Gert Driesen (@drieseng)! I've reopened this so we can explore a separately named, opt-in API without changing existing Assert.IsNotNull calls.

    Assert.GetNotNull is one candidate worth discussing. A custom extension is another option, although extension members cannot override an applicable member already declared on Assert.

    I've opened #12306 with Task/Task<T> regression coverage for the plain, string-message, and interpolated-message overloads, so this compatibility issue is caught in the framework unit-test build.

  15. Evangelink commented on Nov 10, 2026

    @Evangelink
    Member

    Alright so with extension methods, we could do something but it's a little cumbersome as it would force declaring the type argument explictly:

    Assert.IsNotNull(fi.Directory);                         // Existing void method.
    var dir = Assert.IsNotNull(fi.Directory);               // CS0815: cannot assign void.
    var dir = Assert.IsNotNull<DirectoryInfo>(fi.Directory); // Proposed extension works.

    Gert Driesen (@drieseng) would it be something fine for you?

  16. drieseng commented on Nov 20, 2026

    @drieseng
    Author

    Amaury Levé (@Evangelink), I'm afraid there's no good way to deal with this. I'm ok with closing this.

    Sorry for not responding any sooner, busy week both professionally and privately :(

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/assertiontype/breaking-change

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions