-
-
Notifications
You must be signed in to change notification settings - Fork 110
fix: prevent IndexOutOfRangeException in MethodDataSource analyzer #4542
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Add defensive checks to CheckMethodDataSource() to handle cases where attribute constructor arguments are missing or cannot be resolved: - Early return if ConstructorArguments is empty - Safe access to second constructor argument with length check - Early return if method name couldn't be resolved Fixes #4540 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
SummaryFixes crash in analyzer when MethodDataSource attribute has malformed or unresolved constructor arguments during compilation. Critical IssuesNone found ✅ SuggestionsConsider adding a length check for ConstructorArguments[0] access (line 415) While the PR fixes the var type = attribute.AttributeClass?.IsGenericType == true
? attribute.AttributeClass.TypeArguments.First()
: attribute.ConstructorArguments[0].Value as INamedTypeSymbol ?? testClassType;The guard check on line 413-416 (after the fix) returns early if the array is empty, which protects line 415. However, for defensive coding and clarity, you might want to reorder the logic to check the array before any access, or add an explicit length check before line 415. Example: if (attribute.ConstructorArguments.IsDefaultOrEmpty || attribute.ConstructorArguments.Length == 0)
{
return;
}
var type = attribute.AttributeClass?.IsGenericType == true
? attribute.AttributeClass.TypeArguments.First()
: (attribute.ConstructorArguments.Length > 0 ? attribute.ConstructorArguments[0].Value as INamedTypeSymbol : null) ?? testClassType;This is a minor suggestion - the current fix does protect against the crash through the early return. Verdict✅ APPROVE - No critical issues The fix appropriately addresses the reported bug by adding defensive checks for missing constructor arguments. The analyzer will now gracefully skip validation when attributes are malformed due to compilation errors, allowing the compiler to report the actual error instead of causing an analyzer crash. |
Summary
IndexOutOfRangeExceptioncrash inTestDataAnalyzer.CheckMethodDataSource()when attribute constructor arguments are missing or cannot be resolvedRoot Cause
The analyzer was accessing
ConstructorArguments[0]andConstructorArguments[1]without checking if the array had elements. This caused crashes when:The exception was being caught by the analyzer framework and converted into a cryptic
TUnit0001error message.Test plan
Fixes #4540
🤖 Generated with Claude Code