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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 1 addition & 3 deletions src/Features/JsonPatch/src/Internal/DictionaryAdapterOfTU.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public class DictionaryAdapter<TKey, TValue> : IAdapter
}

// As per JsonPatch spec, the target location must exist for test to be successful
if (!dictionary.ContainsKey(convertedKey))
if (!dictionary.TryGetValue(convertedKey, out var currentValue))
{
errorMessage = Resources.FormatTargetLocationAtPathSegmentNotFound(segment);
return false;
Expand All @@ -158,8 +158,6 @@ public class DictionaryAdapter<TKey, TValue> : IAdapter
return false;
}

var currentValue = dictionary[convertedKey];

// The target segment does not have an assigned value to compare the test value with
if (currentValue == null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.

{
return;
}
lock (_lock)
{
if (_schemes.ContainsKey(name))
if (_schemes.TryGetValue(name, out scheme))
{
var scheme = _schemes[name];
if (_requestHandlers.Remove(scheme))
{
_requestHandlersCopy = _requestHandlers.ToArray();
Expand Down
11 changes: 3 additions & 8 deletions src/Identity/Core/src/SignInManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -652,26 +652,21 @@ public virtual async Task<IEnumerable<AuthenticationScheme>> GetExternalAuthenti
{
var auth = await Context.AuthenticateAsync(IdentityConstants.ExternalScheme);
var items = auth?.Properties?.Items;
if (auth?.Principal == null || items == null || !items.ContainsKey(LoginProviderKey))
if (auth?.Principal == null || items == null || !items.TryGetValue(LoginProviderKey, out var provider))
{
return null;
}

if (expectedXsrf != null)
{
if (!items.ContainsKey(XsrfKey))
{
return null;
}
var userId = items[XsrfKey] as string;
if (userId != expectedXsrf)
if (!items.TryGetValue(XsrfKey, out var userId) ||
userId != expectedXsrf)
{
return null;
}
}

var providerKey = auth.Principal.FindFirstValue(ClaimTypes.NameIdentifier);
var provider = items[LoginProviderKey] as string;
if (providerKey == null || provider == null)
{
return null;
Expand Down