Skip to content

Commit d3d853f

Browse files
Bart KoelmanBart Koelman
authored andcommitted
Fixed: do not report AV1008 on program entry point
1 parent 4a83cd0 commit d3d853f

File tree

4 files changed

+42
-11
lines changed

4 files changed

+42
-11
lines changed

src/CSharpGuidelinesAnalyzer/CSharpGuidelinesAnalyzer.Test/Specs/ClassDesign/AvoidStaticClassSpecs.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using CSharpGuidelinesAnalyzer.Rules.ClassDesign;
22
using CSharpGuidelinesAnalyzer.Test.TestDataBuilders;
3+
using Microsoft.CodeAnalysis;
34
using Microsoft.CodeAnalysis.Diagnostics;
45
using Xunit;
56

@@ -298,6 +299,26 @@ static SomeExtensions()
298299
VerifyGuidelineDiagnostic(source);
299300
}
300301

302+
[Fact]
303+
internal void When_static_class_contains_entry_point_it_must_be_skipped()
304+
{
305+
// Arrange
306+
ParsedSourceCode source = new TypeSourceCodeBuilder()
307+
.WithOutputKind(OutputKind.WindowsApplication)
308+
.InGlobalScope(@"
309+
static class Program
310+
{
311+
static void Main(string[] args)
312+
{
313+
}
314+
}
315+
")
316+
.Build();
317+
318+
// Act and assert
319+
VerifyGuidelineDiagnostic(source);
320+
}
321+
301322
protected override DiagnosticAnalyzer CreateAnalyzer()
302323
{
303324
return new AvoidStaticClassAnalyzer();

src/CSharpGuidelinesAnalyzer/CSharpGuidelinesAnalyzer/Extensions/SymbolExtensions.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,5 +370,14 @@ public static string MemberNameWithoutExplicitInterfacePrefix([NotNull] this ISy
370370
int index = symbol.Name.LastIndexOf(".", StringComparison.Ordinal);
371371
return index != -1 ? symbol.Name.Substring(index + 1) : symbol.Name;
372372
}
373+
374+
public static bool IsEntryPoint([NotNull] this IMethodSymbol method, [NotNull] Compilation compilation,
375+
CancellationToken cancellationToken)
376+
{
377+
IMethodSymbol entryPoint =
378+
method.MethodKind == MethodKind.Ordinary ? compilation.GetEntryPoint(cancellationToken) : null;
379+
380+
return Equals(method, entryPoint);
381+
}
373382
}
374383
}

src/CSharpGuidelinesAnalyzer/CSharpGuidelinesAnalyzer/Rules/ClassDesign/AvoidStaticClassAnalyzer.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Collections.Immutable;
44
using System.Linq;
5+
using System.Threading;
56
using CSharpGuidelinesAnalyzer.Extensions;
67
using JetBrains.Annotations;
78
using Microsoft.CodeAnalysis;
@@ -62,14 +63,23 @@ private static void AnalyzeNamedType(SymbolAnalysisContext context)
6263

6364
if (!type.Name.EndsWith("Extensions", StringComparison.Ordinal))
6465
{
65-
context.ReportDiagnostic(Diagnostic.Create(TypeRule, type.Locations[0], type.Name));
66+
if (!TypeContainsEntryPoint(type, context.Compilation, context.CancellationToken))
67+
{
68+
context.ReportDiagnostic(Diagnostic.Create(TypeRule, type.Locations[0], type.Name));
69+
}
6670
}
6771
else
6872
{
6973
AnalyzeTypeMembers(type, context);
7074
}
7175
}
7276

77+
private static bool TypeContainsEntryPoint([NotNull] INamedTypeSymbol type, [NotNull] Compilation compilation,
78+
CancellationToken cancellationToken)
79+
{
80+
return type.GetMembers().OfType<IMethodSymbol>().Any(method => method.IsEntryPoint(compilation, cancellationToken));
81+
}
82+
7383
private static void AnalyzeTypeMembers([NotNull] INamedTypeSymbol type, SymbolAnalysisContext context)
7484
{
7585
IEnumerable<ISymbol> accessibleMembers = type.GetMembers().Where(IsPublicOrInternal)

src/CSharpGuidelinesAnalyzer/CSharpGuidelinesAnalyzer/Rules/Naming/SuffixAsyncMethodCorrectlyAnalyzer.cs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -69,16 +69,7 @@ private static bool RequiresReport([NotNull] IMethodSymbol method, [NotNull] Com
6969
CancellationToken cancellationToken)
7070
{
7171
return method.IsAsync && !method.Name.EndsWith("Async", StringComparison.Ordinal) && !method.IsSynthesized() &&
72-
!IsEntryPoint(method, compilation, cancellationToken);
73-
}
74-
75-
private static bool IsEntryPoint([NotNull] IMethodSymbol method, [NotNull] Compilation compilation,
76-
CancellationToken cancellationToken)
77-
{
78-
IMethodSymbol entryPoint =
79-
method.MethodKind == MethodKind.Ordinary ? compilation.GetEntryPoint(cancellationToken) : null;
80-
81-
return Equals(method, entryPoint);
72+
!method.IsEntryPoint(compilation, cancellationToken);
8273
}
8374

8475
private static void ReportAt([NotNull] IMethodSymbol method, [NotNull] Action<Diagnostic> reportDiagnostic)

0 commit comments

Comments
 (0)