Skip to content

Commit

Permalink
CA1854 avoid double dictionary lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
davidnmbond committed Jan 3, 2025
1 parent 5bd8316 commit 3510e0a
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 21 deletions.
4 changes: 2 additions & 2 deletions PanoramicData.Blazor.Demo/Data/TestFileSystemDataProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -270,13 +270,13 @@ public async Task<OperationResponse> UpdateAsync(FileExplorerItem item, IDiction

await Task.Run(() =>
{
if (!delta.ContainsKey("Path"))
if (!delta.TryGetValue("Path", out object? value))
{
result.ErrorMessage = "Only Path property update supported";
return;
}

var tempPath = delta["Path"]?.ToString() ?? string.Empty;
var tempPath = value?.ToString() ?? string.Empty;
var tempItem = new FileExplorerItem { Path = tempPath ?? string.Empty, Name = FileExplorerItem.GetNameFromPath(tempPath) };
var targetNode = _root.Where(x => x.Path() == tempItem.Path).FirstOrDefault();
var targetParentNode = targetNode is null ? _root.Where(x => x.Path() == tempItem.ParentPath).FirstOrDefault() : targetNode.Parent;
Expand Down
3 changes: 1 addition & 2 deletions PanoramicData.Blazor.Demo/Shared/DemoSourceView.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,8 @@ private async Task<string> LoadSourceAsync(string url)

private async Task OnFileClick(string name)
{
if (_sourceFiles.ContainsKey(name))
if (_sourceFiles.TryGetValue(name, out SourceFile? sourceFile))
{
var sourceFile = _sourceFiles[name];
if (string.IsNullOrWhiteSpace(sourceFile.Content))
{
sourceFile.Content = await LoadSourceAsync(sourceFile.Url).ConfigureAwait(true);
Expand Down
4 changes: 2 additions & 2 deletions PanoramicData.Blazor/Extensions/IQueryableExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ public static IQueryable<T> ApplyFilter<T>(this IQueryable<T> query, Filter filt
// determine property name to use in query
if (string.IsNullOrEmpty(filter.PropertyName))
{
if (keyProperyMappings != null && keyProperyMappings.ContainsKey(filter.Key))
if (keyProperyMappings != null && keyProperyMappings.TryGetValue(filter.Key, out string? value))
{
filter.PropertyName = keyProperyMappings[filter.Key];
filter.PropertyName = value;
}
else
{
Expand Down
4 changes: 2 additions & 2 deletions PanoramicData.Blazor/Models/DataProviderBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,9 @@ public virtual Expression<Func<T, bool>> ApplyFilter(Expression<Func<T, bool>>?
// determine property name to use in query
if (string.IsNullOrEmpty(filter.PropertyName))
{
if (keyPropertyMappings != null && keyPropertyMappings.ContainsKey(filter.Key))
if (keyPropertyMappings != null && keyPropertyMappings.TryGetValue(filter.Key, out string? value))
{
filter.PropertyName = keyPropertyMappings[filter.Key];
filter.PropertyName = value;
}
else
{
Expand Down
6 changes: 3 additions & 3 deletions PanoramicData.Blazor/PDFileExplorer.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1865,14 +1865,14 @@ private async Task GetUploadConflictsAsync(MoveCopyArgs args)
CachedResult<Task<DataResponse<FileExplorerItem>>>? cachedTask = null;
lock (_conflictCache)
{
if (_conflictCache.ContainsKey(folderPath) && _conflictCache[folderPath].HasExpired)
if (_conflictCache.TryGetValue(folderPath, out CachedResult<Task<DataResponse<FileExplorerItem>>>? value) && value.HasExpired)
{
_conflictCache.Remove(folderPath);
}

if (_conflictCache.ContainsKey(folderPath))
if (_conflictCache.TryGetValue(folderPath, out CachedResult<Task<DataResponse<FileExplorerItem>>>? value))

Check failure on line 1873 in PanoramicData.Blazor/PDFileExplorer.razor.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

A local variable or function named 'value' is already defined in this scope

Check failure on line 1873 in PanoramicData.Blazor/PDFileExplorer.razor.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

A local variable or function named 'value' is already defined in this scope

Check failure on line 1873 in PanoramicData.Blazor/PDFileExplorer.razor.cs

View workflow job for this annotation

GitHub Actions / deploy-to-github-pages

A local variable or function named 'value' is already defined in this scope
{
cachedTask = _conflictCache[folderPath];
cachedTask = value;
}
else
{
Expand Down
8 changes: 4 additions & 4 deletions PanoramicData.Blazor/PDForm.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -448,9 +448,9 @@ private async Task ApplyDelta(TItem item)
var memberInfo = field.Field?.GetPropertyMemberInfo();
if (memberInfo is PropertyInfo propInfo)
{
if (updatedValue && Delta.ContainsKey(memberInfo.Name))
if (updatedValue && Delta.TryGetValue(memberInfo.Name, out object? value))

Check failure on line 451 in PanoramicData.Blazor/PDForm.razor.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

A local or parameter named 'value' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter

Check failure on line 451 in PanoramicData.Blazor/PDForm.razor.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

A local or parameter named 'value' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter

Check failure on line 451 in PanoramicData.Blazor/PDForm.razor.cs

View workflow job for this annotation

GitHub Actions / deploy-to-github-pages

A local or parameter named 'value' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter
{
value = Delta[memberInfo.Name];
value = value;

Check warning on line 453 in PanoramicData.Blazor/PDForm.razor.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

Assignment made to same variable; did you mean to assign something else?

Check warning on line 453 in PanoramicData.Blazor/PDForm.razor.cs

View workflow job for this annotation

GitHub Actions / deploy-to-github-pages

Assignment made to same variable; did you mean to assign something else?
}
else
{
Expand Down Expand Up @@ -581,9 +581,9 @@ public string GetFieldStringValue(FormField<TItem> field, bool updatedValue)
var memberInfo = field.Field?.GetPropertyMemberInfo();
if (memberInfo is PropertyInfo propInfo)
{
if (updatedValue && Delta.ContainsKey(memberInfo.Name))
if (updatedValue && Delta.TryGetValue(memberInfo.Name, out object? value))

Check failure on line 584 in PanoramicData.Blazor/PDForm.razor.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

A local or parameter named 'value' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter

Check failure on line 584 in PanoramicData.Blazor/PDForm.razor.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

A local or parameter named 'value' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter

Check failure on line 584 in PanoramicData.Blazor/PDForm.razor.cs

View workflow job for this annotation

GitHub Actions / deploy-to-github-pages

A local or parameter named 'value' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter
{
value = Delta[memberInfo.Name];
value = value;

Check warning on line 586 in PanoramicData.Blazor/PDForm.razor.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

Assignment made to same variable; did you mean to assign something else?

Check warning on line 586 in PanoramicData.Blazor/PDForm.razor.cs

View workflow job for this annotation

GitHub Actions / deploy-to-github-pages

Assignment made to same variable; did you mean to assign something else?
}
else
{
Expand Down
8 changes: 3 additions & 5 deletions PanoramicData.Blazor/PDTable.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -669,9 +669,9 @@ public async Task CommitEditAsync()
var args = new TableAfterEditEventArgs<TItem>(EditItem);
foreach (var column in ActualColumnsToDisplay)
{
if (_editValues.ContainsKey(column.Id) && IsColumnInEditMode(column, EditItem))
if (_editValues.TryGetValue(column.Id, out object? value) && IsColumnInEditMode(column, EditItem))
{
args.NewValues.Add(column.Id, _editValues[column.Id]);
args.NewValues.Add(column.Id, value);
}
}

Expand All @@ -682,10 +682,8 @@ public async Task CommitEditAsync()
// apply edit value to each column
foreach (var column in ActualColumnsToDisplay)
{
if (args.NewValues.ContainsKey(column.Id))
if (args.NewValues.TryGetValue(column.Id, out object? newValue))
{
// get new value (possible updated by host app) and attempt to set on edit item
var newValue = args.NewValues[column.Id];
try
{
column.SetValue(EditItem, newValue);
Expand Down
2 changes: 1 addition & 1 deletion PanoramicData.Blazor/PDTree.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,7 @@ private void UpdateModel(IEnumerable<TItem> items)
}
else
{
parentNode = (dict.ContainsKey(parentKey)) ? parentNode = dict[parentKey] : RootNode.Find(parentKey);
parentNode = (dict.TryGetValue(parentKey, out TreeNode<TItem>? value)) ? parentNode = value : RootNode.Find(parentKey);
}

if (parentNode == null)
Expand Down

0 comments on commit 3510e0a

Please sign in to comment.