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
@@ -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
|
||||
{
|
||||
|
||||
@@ -113,9 +113,8 @@ public class StatementParser : SqlServerStatementParser
|
||||
// PostgreSQL-specific: Append LIMIT/OFFSET to ORDER BY if present
|
||||
var orderByClause = clauses.OrderByClause?.Clause ?? string.Empty;
|
||||
|
||||
if (clausePositions.ContainsKey(KeywordLimit))
|
||||
if (clausePositions.TryGetValue(KeywordLimit, out int limitStart))
|
||||
{
|
||||
var limitStart = clausePositions[KeywordLimit];
|
||||
var limitEnd = clausePositions.Values
|
||||
.Where(v => v > limitStart)
|
||||
.Order()
|
||||
@@ -127,9 +126,8 @@ public class StatementParser : SqlServerStatementParser
|
||||
: $"{orderByClause} {limitClause}";
|
||||
}
|
||||
|
||||
if (clausePositions.ContainsKey(KeywordOffset))
|
||||
if (clausePositions.TryGetValue(KeywordOffset, out int offsetStart))
|
||||
{
|
||||
var offsetStart = clausePositions[KeywordOffset];
|
||||
var offsetEnd = clausePositions.Values
|
||||
.Where(v => v > offsetStart)
|
||||
.Order()
|
||||
|
||||
@@ -119,9 +119,9 @@ public class StatementParser : SqlServerStatementParser
|
||||
protected override void PostProcessClauses(SqlClauses clauses, string sql, Dictionary<string, int> clausePositions)
|
||||
{
|
||||
// Snowflake-specific: Append LIMIT to ORDER BY if present
|
||||
if (clausePositions.ContainsKey(KeywordLimit))
|
||||
if (clausePositions.TryGetValue(KeywordLimit, out int value))
|
||||
{
|
||||
var limitClause = sql.Substring(clausePositions[KeywordLimit]).Trim();
|
||||
var limitClause = sql.Substring(value).Trim();
|
||||
if (clauses.OrderByClause != null)
|
||||
{
|
||||
clauses.OrderByClause.Clause = string.IsNullOrEmpty(clauses.OrderByClause.Clause)
|
||||
|
||||
@@ -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