SonarSource / sonar-dotnet

Code analyzer for C# and VB.NET projects
https://redirect.sonarsource.com/plugins/csharp.html
GNU Lesser General Public License v3.0
791 stars 228 forks source link

Fix S3966: Rule should not throw cast exception #587

Closed jcurl closed 7 years ago

jcurl commented 7 years ago

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

valhristov commented 7 years ago

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

jcurl commented 7 years ago

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.

Evangelink commented 7 years ago

Hi @jcurl,

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

ohadschn commented 7 years ago

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

Evangelink commented 7 years ago

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