Skip to content

Commit 994768b

Browse files
authored
Merge pull request #119 from bkoelman/native-methods
Adds exception for platform invoke wrappers to AV1008
2 parents bca736b + f42b02c commit 994768b

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,37 @@ static void Main(string[] args)
319319
VerifyGuidelineDiagnostic(source);
320320
}
321321

322+
[Fact]
323+
internal void When_static_class_is_platform_invoke_wrapper_it_must_be_skipped()
324+
{
325+
// Arrange
326+
ParsedSourceCode source = new TypeSourceCodeBuilder()
327+
.InGlobalScope(@"
328+
static class NativeMethods
329+
{
330+
}
331+
332+
static class SafeNativeMethods
333+
{
334+
}
335+
336+
static class UnsafeNativeMethods
337+
{
338+
}
339+
340+
public class Wrapper
341+
{
342+
private static class NativeMethods
343+
{
344+
}
345+
}
346+
")
347+
.Build();
348+
349+
// Act and assert
350+
VerifyGuidelineDiagnostic(source);
351+
}
352+
322353
protected override DiagnosticAnalyzer CreateAnalyzer()
323354
{
324355
return new AvoidStaticClassAnalyzer();

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ public sealed class AvoidStaticClassAnalyzer : DiagnosticAnalyzer
4141
private static readonly Action<SymbolAnalysisContext> AnalyzeNamedTypeAction = context =>
4242
context.SkipEmptyName(AnalyzeNamedType);
4343

44+
[ItemNotNull]
45+
private static readonly ImmutableArray<string> PlatformInvokeWrapperTypeNames =
46+
ImmutableArray.Create("NativeMethods", "SafeNativeMethods", "UnsafeNativeMethods");
47+
4448
[ItemNotNull]
4549
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(TypeRule, MemberRule);
4650

@@ -56,7 +60,7 @@ private static void AnalyzeNamedType(SymbolAnalysisContext context)
5660
{
5761
var type = (INamedTypeSymbol)context.Symbol;
5862

59-
if (!type.IsStatic || type.IsSynthesized())
63+
if (!type.IsStatic || type.IsSynthesized() || IsPlatformInvokeWrapper(type))
6064
{
6165
return;
6266
}
@@ -74,6 +78,11 @@ private static void AnalyzeNamedType(SymbolAnalysisContext context)
7478
}
7579
}
7680

81+
private static bool IsPlatformInvokeWrapper([NotNull] INamedTypeSymbol type)
82+
{
83+
return PlatformInvokeWrapperTypeNames.Contains(type.Name);
84+
}
85+
7786
private static bool TypeContainsEntryPoint([NotNull] INamedTypeSymbol type, [NotNull] Compilation compilation,
7887
CancellationToken cancellationToken)
7988
{

0 commit comments

Comments
 (0)