InvalidCastException requesting fixes for SA1129 on target typed new #3277
Closed
Description
struct S
{
internal static S F()
{
S s = new();
return s;
}
}
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Library</OutputType>
<TargetFrameworks>net5.0</TargetFrameworks>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.312">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
</Project>
Double click on the SA1129 (I have it set to error) and then Ctrl-. to go to fix it and you consistently get
System.InvalidCastException : Unable to cast object of type 'Microsoft.CodeAnalysis.CSharp.Syntax.ImplicitObjectCreationExpressionSyntax' to type 'Microsoft.CodeAnalysis.CSharp.Syntax.ObjectCreationExpressionSyntax'.
at StyleCop.Analyzers.ReadabilityRules.SA1129CodeFixProvider.GetReplacementNode(SyntaxNode node,SemanticModel semanticModel,CancellationToken cancellationToken)
at async StyleCop.Analyzers.ReadabilityRules.SA1129CodeFixProvider.GetTransformedDocumentAsync(<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>)
Expected: replace new()
with default
without failing.