Uh oh!
There was an error while loading. Please reload this page.
Use [NotNullWhen(true)] in more places - #47598
Conversation
ghost
commented
Jan 28, 2021
Note regarding the 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
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. |
buyaa-n
left a comment
There was a problem hiding this comment.
Left a few comment, overall looks good to me
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
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.
ce82919 to
608d70cCompare
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: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