Skip to content

Duplicate syntax node callbacks for top-level statements #58561

Closed
SonarSource/sonar-dotnet
#5903

Description

@mavasani

Here is a seemingly suspicious assert:

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 FireCount3 and FireCount5 are 1

Here is where those counters are incremented:

context.RegisterSyntaxNodeAction(Handle2, SyntaxKind.CompilationUnit);

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 Handle2 was called for the same syntax node but different ContainingSymbol.

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

  1. ghost added
    untriaged
    on Jan 3, 2022
  2. self-assigned this
    on Jan 3, 2022
  3. added this to the 17.1 milestone on Jan 3, 2022
  4. added and removed
    untriaged
    on Jan 3, 2022
  5. mavasani commented on Jan 3, 2022

    @mavasani
    ContributorAuthor
  6. added a commit that references this issue on Jan 25, 2022
    6f20654
  7. modified the milestones: 17.1, on Jan 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions