Skip to content

Conversation

@stephentoub
Copy link
Member

The issue fixed by #47579 inspired me to search for other places the consuming experience could be improved by using [NotNullWhen(true)], and found a lot. Most of these are overrides of object.Equals, but some are TryParse methods, other Equals overloads, and a smattering of other APIs. I essentially just did a quick regex search for methods that returned bool and took nullable reference type as the first input, and then added [NotNullWhen(true)] where it was obviously correct (I skipped over a few large areas, namely reflection, as this was already getting a bit out of hand).

For reference, the benefit of applying [NotNullWhen(true)] to a nullable argument is it lets the compiler know that if the method returns true then that argument wasn't null when the method was called, which lets you write code like:

if (string1.Equals(string2))
{
    Console.WriteLine(string2.Length); // no nullable warning because the compiler sees string2 is non-null
}

Note that for the most part I stayed away from modifying virtual methods, but obviously some of the overrides I modified aren't sealed, so technically this could be a breaking change (if someone derives from one of those types and overrides Equals and has nullable enabled and doesn't have [NotNullWhen(true)] on the argument). I can undo any folks feel queasy about.

cc: @jeffhandley, @krwq, @buyaa-n

@ghost
Copy link

ghost commented Jan 28, 2021

Note regarding the new-api-needs-documentation label:

This serves as a reminder for when your PR is modifying a ref *.cs file and adding/modifying public APIs, to please make sure the API implementation in the src *.cs file is documented with triple slash comments, so the PR reviewers can sign off that change.

@ghost
Copy link

ghost commented Jan 28, 2021

I couldn't figure out the best area label to add to this PR. If you have write-permissions please help me learn by adding exactly one area label.

Copy link
Contributor

@buyaa-n buyaa-n left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left a few comment, overall looks good to me

Copy link
Member

@krwq krwq left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM with minor comment

Did a quick search/audit for methods that returned bool and took nullable object as the first input, and added [NotNullWhen(true)] where it was obviously correct.
@stephentoub stephentoub merged commit 2b0e278 into dotnet:master Jan 30, 2021
@stephentoub stephentoub deleted the morenotnullwhentrue branch January 30, 2021 11:09
@ghost ghost locked as resolved and limited conversation to collaborators Mar 1, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

No open projects

Development

Successfully merging this pull request may close these issues.

4 participants