Skip to content

Don't suggest to add null checks to nullable reference types#53249

Merged
jasonmalinowski merged 1 commit intodotnet:release/dev16.11from
jasonmalinowski:do-not-add-null-checks-to-nullable-parameters
May 7, 2021
Merged

Don't suggest to add null checks to nullable reference types#53249
jasonmalinowski merged 1 commit intodotnet:release/dev16.11from
jasonmalinowski:do-not-add-null-checks-to-nullable-parameters

Conversation

@jasonmalinowski
Copy link
Member

If a parameter is marked as nullable, then we shouldn't be adding a check for it to be non-null.

If a parameter is marked as nullable, then we shouldn't be adding
a check for it to be non-null.
@jasonmalinowski jasonmalinowski requested a review from a team as a code owner May 7, 2021 03:42
@ghost ghost added the Area-IDE label May 7, 2021
@jasonmalinowski jasonmalinowski self-assigned this May 7, 2021
Copy link
Member

@Youssef1313 Youssef1313 left a comment

Choose a reason for hiding this comment

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

The refactoring can still be useful in a very limited number of cases (apply it, then manually extend the check). Example:

https://github.com/dotnet/runtime/blob/df6da138702c76f19299627b1ee89837d16ed219/src/libraries/System.Collections/src/System/Collections/Generic/SortedList.cs#L273

However, it should be very rare, so I'm not opposing the change.

@jasonmalinowski
Copy link
Member Author

@jinujoseph: this would need your approval for going into 16.11. Easy bugfix, very low risk, and honestly kinda embarrassing we missed this. 😄

@jasonmalinowski
Copy link
Member Author

@Youssef1313: interesting case! Yep, that's rare and also in this case honestly that's only mattering in the non-generic interface which is more or less something we pretend doesn't exist.

@CyrusNajmabadi
Copy link
Contributor

i don't think this meets any sort of bar . it's a refactoring. i think it was fine that it was offered before. it's not great, but it's nbd imo.

@jasonmalinowski
Copy link
Member Author

@CyrusNajmabadi: this was in my way for the "apply to all" case in #53246 -- it meant I couldn't use "add checks for all" at the public entrypoints after annotating without immediately auditing to make sure it didn't add one I didn't want.

@CyrusNajmabadi
Copy link
Contributor

it meant I couldn't use "add checks for all" at the public entrypoints after annotating without immediately auditing to make sure it didn't add one I didn't want.

I understand :) it's just not something i think meets any sort of bar here. It's in line with all our other refactorings that do something wrong some of the time.

Comment on lines +240 to +248
if (parameter.Type.IsReferenceType)
{
// Don't add null checks to things explicitly declared nullable
if (parameter.Type.NullableAnnotation == NullableAnnotation.Annotated)
{
return false;
}
}
else if (!parameter.Type.IsNullable())
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if (parameter.Type.IsReferenceType)
{
// Don't add null checks to things explicitly declared nullable
if (parameter.Type.NullableAnnotation == NullableAnnotation.Annotated)
{
return false;
}
}
else if (!parameter.Type.IsNullable())
// Don't add null checks to things explicitly declared nullable
if (parameter.Type is { IsReferenceType: true, NullableAnnotation: NullableAnnotation.Annotated })
return false;
if (!parameter.Type.IsNullable())

Copy link
Member Author

@jasonmalinowski jasonmalinowski May 7, 2021

Choose a reason for hiding this comment

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

@CyrusNajmabadi That will change behavior -- IsNullable is specifically "is this Nullable<T>".

Copy link
Member Author

Choose a reason for hiding this comment

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

(might be worth renaming the helper -- let's take that in a separate PR if desired.)

@jinujoseph jinujoseph added this to the 16.11 milestone May 7, 2021
@jasonmalinowski jasonmalinowski merged commit d2745be into dotnet:release/dev16.11 May 7, 2021
@jasonmalinowski jasonmalinowski deleted the do-not-add-null-checks-to-nullable-parameters branch May 7, 2021 21:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants