Skip to content

Commit 7251bb3

Browse files
Copilotmeziantou
andcommitted
Refactor invocation validation into separate methods for better readability
Co-authored-by: meziantou <[email protected]>
1 parent d911bd2 commit 7251bb3

File tree

1 file changed

+26
-19
lines changed

1 file changed

+26
-19
lines changed

src/Meziantou.Analyzer/Rules/ArgumentExceptionShouldSpecifyArgumentNameAnalyzer.cs

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -148,37 +148,43 @@ private static void AnalyzeInvocation(OperationAnalysisContext context, INamedTy
148148
if (op.Arguments.Length == 0)
149149
return;
150150

151-
var availableParameterNames = GetParameterNames(op, context.CancellationToken);
152-
153151
// If there's a second argument (paramName) and it's not null, check that instead
154152
if (op.Arguments.Length >= 2)
155153
{
156154
var secondArgument = op.Arguments[1];
157155
if (secondArgument.Parameter?.Type.IsString() == true && secondArgument.Value is not null)
158156
{
159-
// Check if the second argument is a constant string value
160-
if (secondArgument.Value.ConstantValue.HasValue && secondArgument.Value.ConstantValue.Value is string paramNameValue)
161-
{
162-
if (availableParameterNames.Contains(paramNameValue, StringComparer.Ordinal))
163-
{
164-
if (secondArgument.Value is not INameOfOperation)
165-
{
166-
var properties = ImmutableDictionary<string, string?>.Empty.Add(ArgumentExceptionShouldSpecifyArgumentNameAnalyzerCommon.ArgumentNameKey, paramNameValue);
167-
context.ReportDiagnostic(NameofRule, properties, secondArgument.Value);
168-
}
157+
ValidateParamNameArgument(context, op, secondArgument);
158+
return;
159+
}
160+
}
169161

170-
return;
171-
}
162+
ValidateFirstArgument(context, op);
163+
}
172164

173-
context.ReportDiagnostic(Rule, secondArgument, $"'{paramNameValue}' is not a valid parameter name");
174-
return;
175-
}
165+
private static void ValidateParamNameArgument(OperationAnalysisContext context, IInvocationOperation op, IArgumentOperation paramNameArgument)
166+
{
167+
// Check if the argument is a constant string value
168+
if (!paramNameArgument.Value.ConstantValue.HasValue || paramNameArgument.Value.ConstantValue.Value is not string paramNameValue)
169+
return;
176170

177-
// Cannot determine the value of the second argument, so we can't validate it
178-
return;
171+
var availableParameterNames = GetParameterNames(op, context.CancellationToken);
172+
if (availableParameterNames.Contains(paramNameValue, StringComparer.Ordinal))
173+
{
174+
if (paramNameArgument.Value is not INameOfOperation)
175+
{
176+
var properties = ImmutableDictionary<string, string?>.Empty.Add(ArgumentExceptionShouldSpecifyArgumentNameAnalyzerCommon.ArgumentNameKey, paramNameValue);
177+
context.ReportDiagnostic(NameofRule, properties, paramNameArgument.Value);
179178
}
179+
180+
return;
180181
}
181182

183+
context.ReportDiagnostic(Rule, paramNameArgument, $"'{paramNameValue}' is not a valid parameter name");
184+
}
185+
186+
private static void ValidateFirstArgument(OperationAnalysisContext context, IInvocationOperation op)
187+
{
182188
var firstArgument = op.Arguments[0];
183189
if (firstArgument.Value is null)
184190
return;
@@ -199,6 +205,7 @@ private static void AnalyzeInvocation(OperationAnalysisContext context, INamedTy
199205
return;
200206

201207
// Check if this argument name corresponds to a valid parameter in the current scope
208+
var availableParameterNames = GetParameterNames(op, context.CancellationToken);
202209
if (!availableParameterNames.Contains(argumentName, StringComparer.Ordinal))
203210
{
204211
context.ReportDiagnostic(Rule, firstArgument, $"'{argumentName}' is not a valid parameter name");

0 commit comments

Comments
 (0)