Repository navigation
Closed
SonarSource/sonar-dotnet
#5903Labels
Description
Here is a seemingly suspicious assert:
roslyn/src/Compilers/CSharp/Test/Semantic/Semantics/SimpleProgramsTests.cs
Lines 5111 to 5122 in 80d7e32
var text1 = @"System.Console.WriteLine(1);"; var analyzer = new AnalyzerActions_01_Analyzer(); var comp = CreateCompilation(text1, options: TestOptions.DebugExe, parseOptions: DefaultParseOptions); comp.GetAnalyzerDiagnostics(new[] { analyzer }, null).Verify(); Assert.Equal(1, analyzer.FireCount1); Assert.Equal(0, analyzer.FireCount2); Assert.Equal(1, analyzer.FireCount3); Assert.Equal(0, analyzer.FireCount4); Assert.Equal(1, analyzer.FireCount5); Assert.Equal(0, analyzer.FireCount6); Specifically, both
FireCount3andFireCount5are 1Here is where those counters are incremented:
context.RegisterSyntaxNodeAction(Handle2, SyntaxKind.CompilationUnit); roslyn/src/Compilers/CSharp/Test/Semantic/Semantics/SimpleProgramsTests.cs
Lines 5189 to 5199 in 80d7e32
private void Handle2(SyntaxNodeAnalysisContext context) { var model = context.SemanticModel; var unit = (CompilationUnitSyntax)context.Node; var syntaxTreeModel = ((SyntaxTreeSemanticModel)model); switch (unit.ToString()) { case "System.Console.WriteLine(1);": Interlocked.Increment(ref context.ContainingSymbol.Kind == SymbolKind.Namespace ? ref FireCount5 : ref FireCount3); break; So it looks like
Handle2was called for the same syntax node but differentContainingSymbol.
Thanks @Youssef1313. That confirms similar issue exists with top-level statements. I am going to create a separate tracking issue and PR to address that one.
Originally posted by @mavasani in #58482 (comment)
Activity
mavasani commentedon Jan 3, 2022
@Youssef1313 @AlekseyTs
Avoid duplicate syntax node callbacks for CompilationUnitSyntax nodes…