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:
Thom Lamb
2026-05-26 15:46:19 -05:00
co-authored by Claude Opus 4.7
parent f5d539b906
commit 81254c12f9
4 changed files with 9 additions and 13 deletions
@@ -186,9 +186,9 @@ public class QueryCollectionAnalyzer
var table = query.FromClause?.Clause?.Trim();
if (!string.IsNullOrWhiteSpace(table))
{
if (tableUsage.ContainsKey(table))
if (tableUsage.TryGetValue(table, out int value))
{
tableUsage[table]++;
tableUsage[table] = ++value;
}
else
{
@@ -221,9 +221,9 @@ public class QueryCollectionAnalyzer
foreach (var col in columns)
{
var columnName = col.Trim();
if (columnUsage.ContainsKey(columnName))
if (columnUsage.TryGetValue(columnName, out int value))
{
columnUsage[columnName]++;
columnUsage[columnName] = ++value;
}
else
{