Skip to content

Commit dae0928

Browse files
committed
Exclude top level programs
1 parent 3bcf283 commit dae0928

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

tracer/src/Datadog.Trace.Tools.Analyzers/Helpers/ISymbolExtensions.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,4 +128,22 @@ public static bool HasAnyAttribute(this ISymbol symbol, params INamedTypeSymbol?
128128
{
129129
return symbol.GetAttributes(attributeTypesToMatch).Any();
130130
}
131+
132+
/// <summary>
133+
/// Check if the given <paramref name="methodSymbol"/> is an implicitly generated method for top level statements.
134+
/// </summary>
135+
public static bool IsTopLevelStatementsEntryPointMethod([NotNullWhen(true)] this IMethodSymbol? methodSymbol)
136+
=> methodSymbol?.IsStatic == true && methodSymbol.Name switch
137+
{
138+
"$Main" => true,
139+
"<Main>$" => true,
140+
_ => false
141+
};
142+
143+
/// <summary>
144+
/// Check if the given <paramref name="typeSymbol"/> is an implicitly generated type for top level statements.
145+
/// </summary>
146+
public static bool IsTopLevelStatementsEntryPointType([NotNullWhen(true)] this INamedTypeSymbol? typeSymbol)
147+
=> typeSymbol is not null &&
148+
typeSymbol.GetMembers().OfType<IMethodSymbol>().Any(m => m.IsTopLevelStatementsEntryPointMethod());
131149
}

tracer/src/Datadog.Trace.Tools.Analyzers/SealedAnalyzer/SealedAnalyzer.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ private static void OnCompilationStart(CompilationStartAnalysisContext context)
5151
!type.IsAbstract &&
5252
!type.IsStatic &&
5353
!type.IsSealed &&
54-
!type.HasAnyAttribute(attributesToMatch))
54+
!type.HasAnyAttribute(attributesToMatch) &&
55+
!type.IsTopLevelStatementsEntryPointType())
5556
{
5657
candidateTypes.Add(type);
5758
}

tracer/test/Datadog.Trace.Tools.Analyzers.Tests/SealedAnalyzer/SealedAnalyzerTests.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Threading;
88
using System.Threading.Tasks;
99
using Datadog.Trace.Tools.Analyzers.SealedAnalyzer;
10+
using Microsoft.CodeAnalysis;
1011
using Microsoft.CodeAnalysis.Testing;
1112
using Xunit;
1213
using Test = Microsoft.CodeAnalysis.CSharp.Testing.CSharpCodeFixTest<
@@ -285,6 +286,21 @@ internal sealed class {derivedClass}
285286
await Verifier.VerifyAnalyzerAsync(source); // no diagnostics expected
286287
}
287288

289+
[Fact]
290+
public Task TopLevelStatementsProgram()
291+
{
292+
var test = new Test
293+
{
294+
CodeFixTestBehaviors = CodeFixTestBehaviors.SkipLocalDiagnosticCheck,
295+
TestState =
296+
{
297+
Sources = { @"System.Console.WriteLine();", },
298+
OutputKind = OutputKind.ConsoleApplication,
299+
}
300+
};
301+
return test.RunAsync(); // no diagnostics expected
302+
}
303+
288304
[Fact]
289305
public Task PartialClass_ReportedAndFixedAtAllLocations()
290306
{

0 commit comments

Comments
 (0)