Skip to content

Fix Y0002 codefix crash for foreach deconstruction patterns#48

Draft
Copilot wants to merge 2 commits intomainfrom
copilot/y0002-fix-codefix-crash
Draft

Fix Y0002 codefix crash for foreach deconstruction patterns#48
Copilot wants to merge 2 commits intomainfrom
copilot/y0002-fix-codefix-crash

Conversation

Copy link
Copy Markdown

Copilot AI commented Mar 16, 2026

Applying the Y0002 code fix to foreach (var (a, b) in x) crashes with NullReferenceException because model.GetTypeInfo(var).ConvertedType is null in a deconstruction context — var doesn't map to a single type.

Changes

  • UseImplicitOrExplicitTypeCodeFix.cs

    • In RegisterCodeFixesAsync, detect DeclarationExpressionSyntax with ParenthesizedVariableDesignationSyntax and route to a dedicated handler instead of falling through to the GetTypeInfo path
    • Add UseExplicitDeconstructionTypeAsync: iterates each variable in the designation, resolves its type via GetDeclaredSymbol, and constructs a TupleExpressionSyntax replacement
  • UseImplicitOrExplicitTypeAnalyzerTests.cs

    • Remove [Ignore] from TestDeconstruction

Example

// Before (crashes):
foreach (var (a, b) in x) { }

// After applying Y0002 fix:
foreach ((int a, string b) in x) { }

Nested deconstruction patterns (e.g. var ((a, b), c)) are intentionally left without a fix registration until fully supported.

Original prompt

This section details on the original issue you should resolve

<issue_title>Y0002 codefix crash for 'foreach (var (a, b) in x)'</issue_title>
<issue_description>StackTrace:

System.NullReferenceException : Object reference not set to an instance of an object.
   at Microsoft.CodeAnalysis.CSharp.Extensions.ITypeSymbolExtensions.GenerateTypeSyntax(INamespaceOrTypeSymbol symbol,Boolean nameSyntax,Boolean allowVar)
   at Microsoft.CodeAnalysis.CSharp.CodeGeneration.CSharpSyntaxGenerator.TypeExpression(ITypeSymbol typeSymbol)
   at async YAnalyzers.CSharp.UseImplicitOrExplicitTypeCodeFix.UseExplicitTypeAsync(<Unknown Parameters>)
   at async Microsoft.CodeAnalysis.CodeActions.CodeAction.GetChangedSolutionAsync(<Unknown Parameters>)
   at async Microsoft.CodeAnalysis.CodeActions.CodeAction.ComputeOperationsAsync(<Unknown Parameters>)
   at async Microsoft.CodeAnalysis.CodeActions.CodeAction.GetPreviewOperationsAsync(<Unknown Parameters>)
   at async Microsoft.CodeAnalysis.Editor.Implementation.Suggestions.SuggestedAction.GetPreviewResultAsync(<Unknown Parameters>)
   at async Microsoft.CodeAnalysis.Editor.Implementation.Suggestions.SuggestedActionWithNestedFlavors.<>c__DisplayClass11_0.<GetPreviewAsync>b__0(<Unknown Parameters>)
   at async Microsoft.CodeAnalysis.Extensions.IExtensionManagerExtensions.PerformFunctionAsync[T](<Unknown Parameters>)

The form foreach (var (a, b) in x) should be converted into foreach ((TypeA a, TypeB b) in x)</issue_description>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI changed the title [WIP] [Y0002] Fix codefix crash for foreach destructuring Fix Y0002 codefix crash for foreach deconstruction patterns Mar 16, 2026
Copilot AI requested a review from Youssef1313 March 16, 2026 16:38
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.

Y0002 codefix crash for 'foreach (var (a, b) in x)'

2 participants