'CSharpInvertIfCodeRefactoringProvider' encountered an error and has been disabled

  • Versions used (SonarQube, Scanner, Plugin, and any relevant extension)

    • VS2017 15.9.44
    • SonarLint:5.4.0.42421
  • Error observed (wrap logs/code around triple quote ``` for proper formatting)

System.InvalidCastException : Unable to cast object of type 'Microsoft.CodeAnalysis.Operations.LazyIsTypeExpression' to type 'Microsoft.CodeAnalysis.Operations.IBinaryOperation'.
   at Microsoft.CodeAnalysis.CodeRefactorings.InvertIf.AbstractInvertIfCodeRefactoringProvider.GetNegationOfBinaryExpression(SyntaxNode expressionNode,SyntaxGenerator generator,ISyntaxFactsService syntaxFacts,SemanticModel semanticModel,CancellationToken cancellationToken)
   at Microsoft.CodeAnalysis.CodeRefactorings.InvertIf.AbstractInvertIfCodeRefactoringProvider.Negate(SyntaxNode expression,SyntaxGenerator generator,ISyntaxFactsService syntaxFacts,SemanticModel semanticModel,CancellationToken cancellationToken)
   at Microsoft.CodeAnalysis.CSharp.CodeRefactorings.InvertIf.CSharpInvertIfCodeRefactoringProvider.GetRootWithInvertIfStatement(Document document,SemanticModel model,SyntaxNode ifStatementSyntax,CancellationToken cancellationToken)
   at async Microsoft.CodeAnalysis.CodeRefactorings.InvertIf.AbstractInvertIfCodeRefactoringProvider.InvertIfAsync(<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>)
   at Microsoft.VisualStudio.Telemetry.WindowsErrorReporting.WatsonReport.GetClrWatsonExceptionInfo(Exception exceptionObject)
  • Steps to reproduce

Trying to use the suppress to file with the solution option for a “Replace this type-check-and-cast sequence with an ‘as’ and a null check.” warning. Note that this was combined with a “Use pattern matching” suggestion for the same block of code.

Hi @cyates - welcome to the community, and apologies for the delay in responding.

That refactoring is provided by Microsoft, not by SonarSource (all of our code fixes have Sonar in the namespace).

It looks to be the same as this issue which has been fixed. Unfortunately, the fix milestone is v16 i.e. VS2019. You are using the latest version of VS2017, so I guess the fix was not back-ported to VS2017 :frowning:

Hi Duncan, Thank you for looking at this one and for the explanation. I fixed the underlying condition for the finding so am past it on my end, but wanted to report it. Will hopefully move to VS2019 and/or VS2022 in the near future. Thanks again.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.