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

Adding null-conditional operator on IEnumerable.GetEnumerator() #16947

Merged
merged 3 commits into from
Nov 11, 2019
Merged

Adding null-conditional operator on IEnumerable.GetEnumerator() #16947

merged 3 commits into from
Nov 11, 2019

Conversation

smaglio81
Copy link
Contributor

Summary of the changes (Less than 80 chars)

  • Added ?. operator to prevent null reference exception (_items?.GetEnumerator())

Addresses #16938

@dnfclas
Copy link

dnfclas commented Nov 9, 2019

CLA assistant check
All CLA requirements met.

@BrennanConroy
Copy link
Member

@smaglio81
Copy link
Contributor Author

Okay, I've attempted to integrate the recommended updates. Let me know how it looks.

@BrennanConroy
Copy link
Member

Are you interested in fixing CopyTo also?

@smaglio81
Copy link
Contributor Author

smaglio81 commented Nov 11, 2019

In the code snippet, it seemed like line 98 was doing a null check? Also, I'm kind of new to the github resolution system ... since that comment didn't have the conversational display (and resolution button) like the other comments I thought it might have been a momentary concern that was self-resolved.

Let me know if you still want it updated?

https://github.com/aspnet/AspNetCore/blob/d66db3b0fffdcb010914a4d5a8cb4426f5f1c7ce/src/Http/Http/src/Internal/ItemsDictionary.cs#L98-L104

@BrennanConroy
Copy link
Member

line 98 was doing a null check?

Yeah, but the after the null check line 104 uses _items which would null ref.

@smaglio81
Copy link
Contributor Author

Great Catch! Since it seems like .CopyTo wants to copy the items into the dictionary, what if around line 102 EnsureDictionary() was called to initialize _items?

@BrennanConroy
Copy link
Member

It's copying from _items to the passed in array, we don't want to allocate the dictionary.

I think just adding an else would be good.

@smaglio81
Copy link
Contributor Author

Can do!

@smaglio81
Copy link
Contributor Author

smaglio81 commented Nov 11, 2019

So, I started going down the route of using an else statement, but I wanted to format the curly braces in the way the code normally does it. After poking around for an example, it started to seem like else statements weren't used a lot. So, I went back to your original suggestion and used the null-conditional operator.

I also added a unit test for it. I'm not sure if I hit the standards on it.

Sorry for the back and forth, I should have just implemented it on the first review pass.

@BrennanConroy
Copy link
Member

@aspnet-hello
Copy link

This comment was made automatically. If there is a problem contact aspnetcore-build@microsoft.com.

I've triaged the above build. I've created/commented on the following issue(s)
https://github.com/aspnet/AspNetCore-Internal/issues/2730
https://github.com/aspnet/AspNetCore-Internal/issues/3241

@BrennanConroy BrennanConroy merged commit 5b1b8bc into dotnet:master Nov 11, 2019
@BrennanConroy
Copy link
Member

Thanks @smaglio81 !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants