Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use [NotNullWhen(true)] in more places #47598

Merged
merged 2 commits into from
Jan 30, 2021

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

@dotnet-issue-labeler
Copy link

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.

@dotnet-issue-labeler
Copy link

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.
@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