Sonar-dotnet: Fix S3966: Rule should not throw cast exception

Created on 21 Jul 2017  路  5Comments  路  Source: SonarSource/sonar-dotnet

Description

The error code AD0001 shows up:

Severity    Code    Description Project File    Line    Suppression State   Detail Description
Warning AD0001  Analyzer 'SonarAnalyzer.Rules.CSharp.ObjectsShouldNotBeDisposedMoreThanOnce' threw an exception of type 'System.InvalidCastException' with message 'Unable to cast object of type 'Microsoft.CodeAnalysis.CSharp.Syntax.IdentifierNameSyntax' to type 'Microsoft.CodeAnalysis.CSharp.Syntax.MemberAccessExpressionSyntax'.'.    Datastructures      1   Active  Analyzer 'SonarAnalyzer.Rules.CSharp.ObjectsShouldNotBeDisposedMoreThanOnce' threw the following exception:
'Exception occurred with following context:
Compilation: HBAS.Datastructures
SyntaxTree: C:\Users\jcurl\Documents\Programming\HELIOS\helios.base\framework\datastructures\code\Diagnostic\Process.cs
SyntaxNode: private void DisposeWithException ... [MethodDeclarationSyntax]@[30218..30446) (659,8)-(664,9)

System.InvalidCastException: Unable to cast object of type 'Microsoft.CodeAnalysis.CSharp.Syntax.IdentifierNameSyntax' to type 'Microsoft.CodeAnalysis.CSharp.Syntax.MemberAccessExpressionSyntax'.
   at SonarAnalyzer.Rules.CSharp.ObjectsShouldNotBeDisposedMoreThanOnce.ObjectDisposedPointerCheck.VisitInvocationExpression(InvocationExpressionSyntax instruction, ProgramState programState)
   at SonarAnalyzer.Rules.CSharp.ObjectsShouldNotBeDisposedMoreThanOnce.ObjectDisposedPointerCheck.PreProcessInstruction(ProgramPoint programPoint, ProgramState programState)
   at SonarAnalyzer.Helpers.FlowAnalysis.CSharp.ExplodedGraph.VisitInstruction(ExplodedGraphNode node)
   at SonarAnalyzer.Helpers.FlowAnalysis.Common.ExplodedGraph.Walk()
   at SonarAnalyzer.Rules.CSharp.ObjectsShouldNotBeDisposedMoreThanOnce.CheckForMultipleDispose(ExplodedGraph explodedGraph, SyntaxNodeAnalysisContext context)
   at SonarAnalyzer.Helpers.FlowAnalysis.CSharp.FlowAnalysisExtensions.Analyze(CSharpSyntaxNode declarationBody, ISymbol symbol, Action`2 analyze, SyntaxNodeAnalysisContext context)
   at SonarAnalyzer.Helpers.FlowAnalysis.CSharp.FlowAnalysisExtensions.<>c__DisplayClass0_0.<RegisterExplodedGraphBasedAnalysis>b__1(SyntaxNodeAnalysisContext c)
   at SonarAnalyzer.Helpers.DiagnosticAnalyzerContextHelper.<>c__DisplayClass0_0`1.<RegisterSyntaxNodeActionInNonGenerated>b__0(SyntaxNodeAnalysisContext c)
   at SonarAnalyzer.Helpers.SonarAnalysisContext.<>c__DisplayClass12_0`1.<RegisterSyntaxNodeAction>b__0(SyntaxNodeAnalysisContext c)
   at Microsoft.CodeAnalysis.Diagnostics.AnalyzerExecutor.<>c__DisplayClass42_1`1.<ExecuteSyntaxNodeAction>b__1()
   at Microsoft.CodeAnalysis.Diagnostics.AnalyzerExecutor.ExecuteAndCatchIfThrows_NoLock(DiagnosticAnalyzer analyzer, Action analyze, Nullable`1 info)
-----
'.

Repro steps

I'm not able to identify the code location in my project that shows the problem. As the exception indicates a particular file, I'm attaching that.
Process.zip

Expected behavior

No error AD0001

Actual behavior

AD0001 shows up

Known workarounds

Ignoring it for now...

Related information

  • SonarC# Version 3.3.0
  • Visual Studio 2015 Enterprise targeted for .NET 4.0
Bug

All 5 comments

Hi @jcurl, thanks for the feedback, we will provide a fix as soon as possible!

HI, I just want to mention, that I've had to drop down to v3.2.1 of SonarLint as for some projects it only shows this error and then stops scanning in the IDE. So it appears it wasn't there in 3.2.1.

Hi @jcurl,

You are right we changed the way the rule works and it seems like we have some uncovered cases.

@Evangelink thank you for fixing this. When will a SonarAnalyzer.CSharp package with this fix be release?

Hi @ohadschn we will release the RC today and will release the final Wednesday.

Was this page helpful?
0 / 5 - 0 ratings