Skip to content

Conversation

@thomhurst
Copy link
Owner

Summary

  • Fix IndexOutOfRangeException crash in TestDataAnalyzer.CheckMethodDataSource() when attribute constructor arguments are missing or cannot be resolved
  • Add defensive checks to handle malformed attributes gracefully

Root Cause

The analyzer was accessing ConstructorArguments[0] and ConstructorArguments[1] without checking if the array had elements. This caused crashes when:

  • The attribute's constructor arguments couldn't be resolved during compilation
  • There were other compilation errors in the user's code
  • The attribute was malformed

The exception was being caught by the analyzer framework and converted into a cryptic TUnit0001 error message.

Test plan

  • Verify the analyzer no longer crashes with missing constructor arguments
  • Run existing analyzer tests to ensure no regression

Fixes #4540

🤖 Generated with Claude Code

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>
@thomhurst
Copy link
Owner Author

Summary

Fixes crash in analyzer when MethodDataSource attribute has malformed or unresolved constructor arguments during compilation.

Critical Issues

None found ✅

Suggestions

Consider adding a length check for ConstructorArguments[0] access (line 415)

While the PR fixes the IndexOutOfRangeException for ConstructorArguments[1], it still accesses ConstructorArguments[0] without verifying the array has at least one element (line 415 in the diff context):

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.

@thomhurst thomhurst merged commit 1685226 into main Jan 23, 2026
13 checks passed
@thomhurst thomhurst deleted the fix/method-data-source-analyzer-crash branch January 23, 2026 18:59
This was referenced Jan 25, 2026
This was referenced Jan 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: MethodDataSource is not functioning correctly

2 participants