Skip to content

Conversation

@deepakrathore33
Copy link
Contributor

@deepakrathore33 deepakrathore33 commented Dec 3, 2025

Fixes 81530

Problem

VS crashes with InvalidCastException when "Convert to foreach" refactoring analyzes
a for loop where the iteration variable is used inside a tuple:

for (int i = 0; i < array.Length; i++)
{
    var tuple = (array[i], i);  // crash here
}

Cause

TupleExpressionSyntax contains ArgumentSyntax children but is NOT a BaseArgumentListSyntax
The code assumed argument's parent is always a valid argument list.

Fix

Check if it's a valid element access or invocation BEFORE calling GetArgumentsOfArgumentList

Testing

Added TestNotWithIterationVariableInTupleExpression

@deepakrathore33 deepakrathore33 requested a review from a team as a code owner December 3, 2025 16:35
@CyrusNajmabadi
Copy link
Member

Thanks!

@deepakrathore33 deepakrathore33 force-pushed the fix/convert-for-foreach-tuple-crash branch from 7109529 to 66fc624 Compare December 3, 2025 16:59
@deepakrathore33
Copy link
Contributor Author

Fixes: #81608

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Visual Studio 2026 - InvalidCastException

2 participants