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 TryGetValue() for dictionary lookup #44791

Merged
merged 1 commit into from Oct 31, 2022

Conversation

martincostello
Copy link
Member

@martincostello martincostello commented Oct 31, 2022

Use TryGetValue

Use TryGetValue() instead of ContainsKey() and the indexer.

Description

While looking through the Identity code to work out why something I'm doing wasn't working, I spotted a case where TryGetValue() wasn't being used to access a dictionary value. I did a quick search and found a couple more, but I didn't go through the whole repo so there might be more lurking.

Enabling the new CA1854 rule would surface more of this and help stop new ones being added.

Use `TryGetValue()` instead of `ContainsKey()` and the indexer.
@ghost ghost added the community-contribution Indicates that the PR has been added by a community member label Oct 31, 2022
@ghost
Copy link

ghost commented Oct 31, 2022

Thanks for your PR, @martincostello. Someone from the team will get assigned to your PR shortly and we'll get it reviewed.

@BrennanConroy
Copy link
Member

Enabling the new CA1854 rule would surface more of this and help stop new ones being added.

Oh nice! Want to try enabling it (different PR is fine)?
Otherwise I'll probably end up doing it later this week 😁

@martincostello
Copy link
Member Author

Sure - I'll give it a whirl in a new PR and see what happens...

@mkArtakMSFT mkArtakMSFT added the area-identity Includes: Identity and providers label Oct 31, 2022
@martincostello martincostello mentioned this pull request Oct 31, 2022
@@ -184,15 +184,14 @@ public virtual void AddScheme(AuthenticationScheme scheme)
/// <param name="name">The name of the authenticationScheme being removed.</param>
public virtual void RemoveScheme(string name)
{
if (!_schemes.ContainsKey(name))
if (!_schemes.TryGetValue(name, out var scheme))
Copy link
Member

Choose a reason for hiding this comment

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

There is also GetSchemeAsync which can be updated, but that'll probably show up in the other PR.

Edit: Looks like it did show up in the other PR 😄

Copy link
Member

Choose a reason for hiding this comment

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

Should this one be changed given the out value is discarded?
Or:

Suggested change
if (!_schemes.TryGetValue(name, out var scheme))
if (!_schemes.TryGetValue(name, out var _))

Copy link
Member Author

Choose a reason for hiding this comment

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

Looks like #44799 found that and 4 others. Rather than churn this PR, shall I just change this and address those with a rebase once this is merged?

Copy link
Member

Choose a reason for hiding this comment

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

Whatever's convenient for you.

@BrennanConroy BrennanConroy merged commit 53b0980 into dotnet:main Oct 31, 2022
@BrennanConroy
Copy link
Member

Thanks @martincostello! Looking forward to the next PR 😃

@BrennanConroy BrennanConroy added this to the 8.0-preview1 milestone Oct 31, 2022
@martincostello martincostello deleted the identity-try-get-value branch October 31, 2022 20:15
martincostello added a commit to martincostello/aspnetcore that referenced this pull request Oct 31, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-identity Includes: Identity and providers community-contribution Indicates that the PR has been added by a community member Perf
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants