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

remove duplicate dictionary lookups #10999

Merged
merged 2 commits into from
May 31, 2023
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
2 changes: 1 addition & 1 deletion GitCommands/Config/ConfigSection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public string GetValue(string key, string defaultValue)

public IReadOnlyList<string> GetValues(string key)
{
return _configKeys.ContainsKey(key) ? _configKeys[key] : new List<string>();
return _configKeys.TryGetValue(key, out List<string>? configKey) ? configKey : Array.Empty<string>();
}

public override string ToString()
Expand Down
4 changes: 2 additions & 2 deletions GitCommands/DiffMergeTools/RegisteredDiffMergeTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ public static IEnumerable<string> All(DiffMergeToolType toolType)

internal static DiffMergeTool? Get(string toolName)
{
if (RegisteredTools.ContainsKey(toolName))
if (RegisteredTools.TryGetValue(toolName, out DiffMergeTool? tool))
{
return RegisteredTools[toolName];
return tool;
}

return null;
Expand Down
2 changes: 1 addition & 1 deletion GitUI/AutoCompletion/CommitAutoCompleteProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ private IGitModule GetModule()

private static Regex? GetRegexForExtension(string extension)
{
return _regexes.Value.ContainsKey(extension) ? _regexes.Value[extension] : null;
return _regexes.Value.TryGetValue(extension, out Regex? value) ? value : null;
}

private static IEnumerable<string> ReadOrInitializeAutoCompleteRegexes()
Expand Down
4 changes: 2 additions & 2 deletions GitUI/CommandsDialogs/FormBrowse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2567,9 +2567,9 @@ private async Task UpdateSubmoduleMenuStatusAsync(SubmoduleInfoResult result, Ca
continue;
}

if (infos.ContainsKey(path))
if (infos.TryGetValue(path, out SubmoduleInfo? info))
{
UpdateSubmoduleMenuItemStatus(item, infos[path]);
UpdateSubmoduleMenuItemStatus(item, info);
}
else
{
Expand Down
4 changes: 2 additions & 2 deletions GitUI/UserControls/FileStatusList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1124,8 +1124,8 @@ void EnsureSelectedIndexChangeSubscription()
int GetItemImageIndex(GitItemStatus gitItemStatus)
{
var imageKey = GetItemImageKey(gitItemStatus);
return _stateImageIndexDict.ContainsKey(imageKey)
? _stateImageIndexDict[imageKey]
return _stateImageIndexDict.TryGetValue(imageKey, out int value)
? value
: _stateImageIndexDict[nameof(Images.FileStatusUnknown)];
}

Expand Down
4 changes: 2 additions & 2 deletions Plugins/Bitbucket/BitbucketPullRequestForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,9 @@ private async Task<IEnumerable<string>> GetBitbucketBranchesAsync(Repository sel
{
lock (_branches)
{
if (_branches.ContainsKey(selectedRepo))
if (_branches.TryGetValue(selectedRepo, out IEnumerable<string>? selectedBranches))
{
return _branches[selectedRepo];
return selectedBranches;
}
}

Expand Down
16 changes: 8 additions & 8 deletions Plugins/Statistics/GitImpact/ImpactControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -215,17 +215,17 @@ private void OnPaint(object sender, PaintEventArgs e)
// Default: person with least number of changed lines first, others on top
foreach (var author in _authorStack)
{
if (_brushes.ContainsKey(author) && _paths.ContainsKey(author))
if (_brushes.ContainsKey(author) && _paths.TryGetValue(author, out GraphicsPath? authorPath))
{
e.Graphics.FillPath(_brushes[author], _paths[author]);
e.Graphics.FillPath(_brushes[author], authorPath);
}
}

// Draw black border around selected author
string selectedAuthor = _authorStack[_authorStack.Count - 1];
if (_brushes.ContainsKey(selectedAuthor) && _paths.ContainsKey(selectedAuthor))
if (_brushes.ContainsKey(selectedAuthor) && _paths.TryGetValue(selectedAuthor, out GraphicsPath? selectedAuthorPath))
{
e.Graphics.DrawPath(new Pen(SystemColors.WindowText, 2), _paths[selectedAuthor]);
e.Graphics.DrawPath(new Pen(SystemColors.WindowText, 2), selectedAuthorPath);
}

foreach (var author in _authorStack)
Expand Down Expand Up @@ -507,9 +507,9 @@ public Color GetAuthorColor(string author)
{
lock (_dataLock)
{
if (_brushes.ContainsKey(author))
if (_brushes.TryGetValue(author, out SolidBrush? brush))
{
return _brushes[author].Color;
return brush.Color;
}
}

Expand All @@ -532,9 +532,9 @@ public ImpactLoader.DataPoint GetAuthorInfo(string author)
{
lock (_dataLock)
{
if (_authors.ContainsKey(author))
if (_authors.TryGetValue(author, out ImpactLoader.DataPoint info))
{
return _authors[author];
return info;
}

return new ImpactLoader.DataPoint(0, 0, 0);
Expand Down