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 S3881: Rule should not fail with 'Syntax node is not within syntax tree' #690

Closed jcurl closed 7 years ago

jcurl commented 7 years ago

Description

The following warning is generated for a particular class

Severity    Code    Description Project File    Line    Suppression State   Detail Description
Warning AD0001  Analyzer 'SonarAnalyzer.Rules.CSharp.ImplementIDisposableCorrectly' threw an exception of type 'System.ArgumentException' with message 'Syntax node is not within syntax tree'. TestTraceListener       1   Active  Analyzer 'SonarAnalyzer.Rules.CSharp.ImplementIDisposableCorrectly' threw the following exception:
'Exception occurred with following context:
Compilation: HBAS.Automation.TestTraceListener
SyntaxTree: C:\Users\jcurl\Documents\Programming\helios.base\framework\testtracelistener\code\TestCase.MappingCollection.cs
SyntaxNode: public partial class TestCase { ... [ClassDeclarationSyntax]@[142..6895) (7,4)-(147,5)

System.ArgumentException: Syntax node is not within syntax tree
   at Microsoft.CodeAnalysis.CSharp.CSharpSemanticModel.CheckSyntaxNode(CSharpSyntaxNode syntax)
   at Microsoft.CodeAnalysis.CSharp.SyntaxTreeSemanticModel.GetDeclaredMemberSymbol(CSharpSyntaxNode declarationSyntax)
   at Microsoft.CodeAnalysis.CSharp.SyntaxTreeSemanticModel.GetDeclaredSymbol(BaseMethodDeclarationSyntax declarationSyntax, CancellationToken cancellationToken)
   at SonarAnalyzer.Rules.CSharp.ImplementIDisposableCorrectly.DisposableChecker.VerifyDispose(MethodDeclarationSyntax disposeMethod, Boolean isSealedClass)
   at SonarAnalyzer.Rules.CSharp.ImplementIDisposableCorrectly.DisposableChecker.GetIssueLocations()
   at SonarAnalyzer.Rules.CSharp.ImplementIDisposableCorrectly.<>c.<Initialize>b__6_0(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

Please see attached a copy of the two source files that appear to be relevant. TestCaseMapping.zip

Expected behavior

No AD0001 should be raised.

Actual behavior

See problem description. AD0001 is unexpectedly raised

Known workarounds

None known

Related information

Evangelink commented 7 years ago

Hi @jcurl, Thanks for this bug report! We will deal with it in the next version of SonarC#.