Version Used: Visual Studio 2022 Community 17.9.6; .NET 8; C# 12
Steps to Reproduce:
Insert this code:
using System.Security.Cryptography;
using System.Text;
using (var sha256 = SHA256.Create())
{
var result = sha256.ComputeHash(Encoding.UTF8.GetBytes("sample text"));
}
2. Under 'sha256' are 3 dots, the CA1850 information. So I click on "Show Potential Fixes" and hovering over the first option "Replace with 'HashData' method" crashes refactoring, e.g. Visual Studio shows a yellow banner:
'CSharpPreferHashDataOverComputeHashFixer' encountered an error and has been disabled.
![Снимок экрана 2024-05-26 005932](https://github.com/dotnet/roslyn/assets/142818255/267286e1-2dd6-42cf-81f5-cf2472b153c6)
**Diagnostic Id**: [CA1850](https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca1850)
**Expected Behavior**: When hovering over "Replace with 'HashData' method" under Show potential fixes, I should be able to see the preview of the code and apply the code fix without any issues.
**Actual Behavior**: Hovering over "Replace with 'HashData' method" under Show potential fixes does not show the preview and results in the yellow banner described above, and then I can't click "Show potential fixes" for CA1850 anymore until I click "Enable" on the yellow banner.
This is the stack trace:
System.InvalidOperationException : The item specified is not the element of a list.
at Microsoft.CodeAnalysis.CSharp.Syntax.SyntaxReplacer.NodeListEditor.Visit(SyntaxNode node)
at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxRewriter.VisitGlobalStatement(GlobalStatementSyntax node)
at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxRewriter.Visit(SyntaxNode node)
at Microsoft.CodeAnalysis.CSharp.Syntax.SyntaxReplacer.BaseListEditor.Visit(SyntaxNode node)
at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxNode.InsertNodesInListCore(SyntaxNode nodeInList,IEnumerable1 nodesToInsert,Boolean insertBefore) at Microsoft.CodeAnalysis.SyntaxNodeExtensions.InsertNodesBefore[TRoot](TRoot root,SyntaxNode nodeInList,IEnumerable1 newNodes)
at Microsoft.NetCore.CSharp.Analyzers.Performance.CSharpPreferHashDataOverComputeHashFixer.CSharpPreferHashDataOverComputeHashFixHelper.MoveStatementsOutOfUsingStatementWithFormatting(SyntaxNode root,UsingStatementSyntax usingStatement)
at Microsoft.NetCore.CSharp.Analyzers.Performance.CSharpPreferHashDataOverComputeHashFixer.CSharpPreferHashDataOverComputeHashFixHelper.FixHashCreateNode(SyntaxNode root,SyntaxNode createNode)
at Microsoft.NetCore.Analyzers.Performance.PreferHashDataOverComputeHashFixer.PreferHashDataOverComputeHashFixHelper.FixHashInstanceTarget(SyntaxNode root,HashInstanceTarget hashInstanceTarget)
at Microsoft.NetCore.Analyzers.Performance.PreferHashDataOverComputeHashFixer.HashDataCodeAction.GetChangedDocumentAsync(CancellationToken cancellationToken)
at Microsoft.CodeAnalysis.CodeActions.CodeAction.GetChangedDocumentAsync(IProgress`1 progress,CancellationToken cancellationToken)
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.ComputePreviewOperationsAsync()
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.PreviewChangesSuggestedAction.CreateAsync()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at async Microsoft.CodeAnalysis.Editor.Implementation.Suggestions.SuggestedActionWithNestedFlavors.GetPreviewChangesFlavorAsync()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at async Microsoft.CodeAnalysis.Editor.Implementation.Suggestions.SuggestedActionWithNestedFlavors.CreateAllFlavorsAsync()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at async Microsoft.CodeAnalysis.Extensions.IExtensionManagerExtensions.PerformFunctionAsync[T]()
Version Used: Visual Studio 2022 Community 17.9.6; .NET 8; C# 12
Steps to Reproduce:
using (var sha256 = SHA256.Create()) { var result = sha256.ComputeHash(Encoding.UTF8.GetBytes("sample text")); }
'CSharpPreferHashDataOverComputeHashFixer' encountered an error and has been disabled.
System.InvalidOperationException : The item specified is not the element of a list. at Microsoft.CodeAnalysis.CSharp.Syntax.SyntaxReplacer.NodeListEditor.Visit(SyntaxNode node) at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxRewriter.VisitGlobalStatement(GlobalStatementSyntax node) at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxRewriter.Visit(SyntaxNode node) at Microsoft.CodeAnalysis.CSharp.Syntax.SyntaxReplacer.BaseListEditor.Visit(SyntaxNode node) at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxNode.InsertNodesInListCore(SyntaxNode nodeInList,IEnumerable)
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.ComputePreviewOperationsAsync()
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.PreviewChangesSuggestedAction.CreateAsync()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at async Microsoft.CodeAnalysis.Editor.Implementation.Suggestions.SuggestedActionWithNestedFlavors.GetPreviewChangesFlavorAsync()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at async Microsoft.CodeAnalysis.Editor.Implementation.Suggestions.SuggestedActionWithNestedFlavors.CreateAllFlavorsAsync()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at async Microsoft.CodeAnalysis.Extensions.IExtensionManagerExtensions.PerformFunctionAsync[T]()
1 nodesToInsert,Boolean insertBefore) at Microsoft.CodeAnalysis.SyntaxNodeExtensions.InsertNodesBefore[TRoot](TRoot root,SyntaxNode nodeInList,IEnumerable
1 newNodes) at Microsoft.NetCore.CSharp.Analyzers.Performance.CSharpPreferHashDataOverComputeHashFixer.CSharpPreferHashDataOverComputeHashFixHelper.MoveStatementsOutOfUsingStatementWithFormatting(SyntaxNode root,UsingStatementSyntax usingStatement) at Microsoft.NetCore.CSharp.Analyzers.Performance.CSharpPreferHashDataOverComputeHashFixer.CSharpPreferHashDataOverComputeHashFixHelper.FixHashCreateNode(SyntaxNode root,SyntaxNode createNode) at Microsoft.NetCore.Analyzers.Performance.PreferHashDataOverComputeHashFixer.PreferHashDataOverComputeHashFixHelper.FixHashInstanceTarget(SyntaxNode root,HashInstanceTarget hashInstanceTarget) at Microsoft.NetCore.Analyzers.Performance.PreferHashDataOverComputeHashFixer.HashDataCodeAction.GetChangedDocumentAsync(CancellationToken cancellationToken) at Microsoft.CodeAnalysis.CodeActions.CodeAction.GetChangedDocumentAsync(IProgress`1 progress,CancellationToken cancellationToken) at async Microsoft.CodeAnalysis.CodeActions.CodeAction.GetChangedSolutionAsync(