chore(sonar): prefer TryGetValue over ContainsKey+indexer (CA1854)
Applied via `dotnet format analyzers --diagnostics CA1854 --severity info`. Eliminates the duplicate hash lookup in the `if (d.ContainsKey(k)) d[k]++ else d[k] = 1` pattern. The fixer rewrites the conditional to `if (d.TryGetValue(k, out var value)) d[k] = ++value;` which is semantically identical but does the lookup once. 4 files touched. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.7
parent
f5d539b906
commit
81254c12f9
@@ -271,15 +271,13 @@ public static class FlatDataUtils
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(data);
|
||||
|
||||
if (!data.ContainsKey(key))
|
||||
if (!data.TryGetValue(key, out object? rawValue))
|
||||
{
|
||||
throw new ArgumentException(
|
||||
$"The specified key is not available. Requested key: [{key}] Available keys: [{string.Join(", ", data.Keys)}]",
|
||||
nameof(key));
|
||||
}
|
||||
|
||||
var rawValue = data[key];
|
||||
|
||||
return rawValue;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user