Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

InvalidCastException requesting fixes for SA1129 on target typed new #3277

Closed
danmoseley opened this issue Dec 21, 2020 · 3 comments · Fixed by #3278
Closed

InvalidCastException requesting fixes for SA1129 on target typed new #3277

danmoseley opened this issue Dec 21, 2020 · 3 comments · Fixed by #3278

Comments

@danmoseley
Copy link
Contributor

danmoseley commented Dec 21, 2020

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.

@danmoseley
Copy link
Contributor Author

Fix may be just to replace ObjectCreationExpressionSyntax in these places to BaseObjectCreationExpressionSyntax


private static bool IsDefaultParameterValue(ObjectCreationExpressionSyntax expression)

And may review other use of ObjectCreationExpressionSyntax in the project?

@danmoseley
Copy link
Contributor Author

@danmoseley
Copy link
Contributor Author

I don't have time to offer a fix, unfortunately.

sharwell pushed a commit to sharwell/StyleCopAnalyzers that referenced this issue Dec 21, 2020
sharwell added a commit to sharwell/StyleCopAnalyzers that referenced this issue Dec 21, 2020
@sharwell sharwell self-assigned this Dec 21, 2020
sharwell added a commit to sharwell/StyleCopAnalyzers that referenced this issue Dec 21, 2020
@sharwell sharwell added this to the 1.2-beta.next milestone Dec 21, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants