Skip to content

System.InvalidCastException when using the newly introduced "Use Touple to Swap Values" in VS 17.1 to simplify usage of temp variable when swapping variables #58759

@Raviuppa

Description

@Raviuppa

Version Used:
VS 17.1

Steps to Reproduce:

  1. Install VS 2022 Update 1 Preview 2

  2. Create a new .NET Console App

  3. Add Below code to swap variables

     var rslt1 = 0;
     var rslt2= 5;
     
     var tmp = rslt1;
     rslt1 = rslt2;
     rslt2 = tmp;
    
  4. Place the curser on 'tmp' and press 'Ctrl+." to invoke the context menu

  5. Select "Use touples to swap values"

Expected Behavior:
(rslt2, result1) = (rslt1, rslt2);
Actual Behavior:
System.InvalidCastException.

image

System.InvalidCastException : Unable to cast object of type 'Microsoft.CodeAnalysis.CSharp.Syntax.GlobalStatementSyntax' to type 'Microsoft.CodeAnalysis.CSharp.Syntax.LocalDeclarationStatementSyntax'.
at Microsoft.CodeAnalysis.CSharp.UseTupleSwap.CSharpUseTupleSwapCodeFixProvider.FixOne(SyntaxEditor editor,Diagnostic diagnostic,CancellationToken cancellationToken)
at Microsoft.CodeAnalysis.CSharp.UseTupleSwap.CSharpUseTupleSwapCodeFixProvider.FixAllAsync(Document document,ImmutableArray`1 diagnostics,SyntaxEditor editor,CancellationToken cancellationToken)
at Microsoft.CodeAnalysis.CodeFixes.SyntaxEditorBasedCodeFixProvider.<>c__DisplayClass4_0.b__0(SyntaxEditor editor)
at async Microsoft.CodeAnalysis.CodeFixes.SyntaxEditorBasedCodeFixProvider.FixAllWithEditorAsync()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at async Microsoft.CodeAnalysis.CodeActions.CodeAction.GetChangedSolutionAsync()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at async Microsoft.CodeAnalysis.CodeActions.CodeAction.ComputeOperationsAsync()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at async Microsoft.CodeAnalysis.CodeActions.CodeAction.GetPreviewOperationsAsync()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at async Microsoft.CodeAnalysis.Editor.Implementation.Suggestions.SuggestedAction.GetPreviewResultAsync()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at async Microsoft.CodeAnalysis.Editor.Implementation.Suggestions.SuggestedActionWithNestedFlavors.<>c__DisplayClass11_0.b__0()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at async Microsoft.CodeAnalysis.Extensions.IExtensionManagerExtensions.PerformFunctionAsyncT
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at Microsoft.VisualStudio.Telemetry.WindowsErrorReporting.WatsonReport.GetClrWatsonExceptionInfo(Exception exceptionObject)

Metadata

Metadata

Assignees

Labels

Area-IDEIDE-CodeStyleBuilt-in analyzers, fixes, and refactoringsResolution-FixedThe bug has been fixed and/or the requested behavior has been implemented

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions