dotnet / roslyn-analyzers

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Attempting to refactor CA1850 crashes refactoring

winscripter opened this issue · comments

Version Used: Visual Studio 2022 Community 17.9.6; .NET 8; C# 12

Steps to Reproduce:

  1. Insert this code:
using System.Security.Cryptography;
using System.Text;

using (var sha256 = SHA256.Create())
{
    var result = sha256.ComputeHash(Encoding.UTF8.GetBytes("sample text"));
}
  1. 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

Diagnostic Id: 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,IEnumerable`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(<Unknown Parameters>)
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at async Microsoft.CodeAnalysis.CodeActions.CodeAction.ComputeOperationsAsync(<Unknown Parameters>)
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at async Microsoft.CodeAnalysis.CodeActions.CodeAction.ComputePreviewOperationsAsync(<Unknown Parameters>)
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at async Microsoft.CodeAnalysis.CodeActions.CodeAction.GetPreviewOperationsAsync(<Unknown Parameters>)
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at async Microsoft.CodeAnalysis.Editor.Implementation.Suggestions.SuggestedAction.GetPreviewResultAsync(<Unknown Parameters>)
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at async Microsoft.CodeAnalysis.Editor.Implementation.Suggestions.SuggestedActionWithNestedFlavors.PreviewChangesSuggestedAction.CreateAsync(<Unknown Parameters>)
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at async Microsoft.CodeAnalysis.Editor.Implementation.Suggestions.SuggestedActionWithNestedFlavors.GetPreviewChangesFlavorAsync(<Unknown Parameters>)
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at async Microsoft.CodeAnalysis.Editor.Implementation.Suggestions.SuggestedActionWithNestedFlavors.CreateAllFlavorsAsync(<Unknown Parameters>)
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at async Microsoft.CodeAnalysis.Extensions.IExtensionManagerExtensions.PerformFunctionAsync[T](<Unknown Parameters>)

Moving to roslyn-analyzers.