diff --git a/src/Meziantou.Analyzer/Rules/DoNotDeclareStaticMembersOnGenericTypes.cs b/src/Meziantou.Analyzer/Rules/DoNotDeclareStaticMembersOnGenericTypes.cs index cfe54287c..a1894384f 100644 --- a/src/Meziantou.Analyzer/Rules/DoNotDeclareStaticMembersOnGenericTypes.cs +++ b/src/Meziantou.Analyzer/Rules/DoNotDeclareStaticMembersOnGenericTypes.cs @@ -36,8 +36,8 @@ private static void Analyze(SymbolAnalysisContext context) if (member.IsAbstract || member.IsVirtual) continue; - // skip properties - if (member is IMethodSymbol method && (method.MethodKind == MethodKind.PropertyGet || method.MethodKind == MethodKind.PropertySet)) + // The accessors are reported through the property or event they belong to + if (member is IMethodSymbol { MethodKind: MethodKind.PropertyGet or MethodKind.PropertySet or MethodKind.EventAdd or MethodKind.EventRemove }) continue; // skip operators diff --git a/tests/Meziantou.Analyzer.Test/Rules/DoNotDeclareStaticMembersOnGenericTypesTests.cs b/tests/Meziantou.Analyzer.Test/Rules/DoNotDeclareStaticMembersOnGenericTypesTests.cs index 0ab9165d2..5a578c5dd 100644 --- a/tests/Meziantou.Analyzer.Test/Rules/DoNotDeclareStaticMembersOnGenericTypesTests.cs +++ b/tests/Meziantou.Analyzer.Test/Rules/DoNotDeclareStaticMembersOnGenericTypesTests.cs @@ -85,6 +85,38 @@ public class Test return test.RunAsync(); } + [Fact] + public Task StaticMembers_Event() + { + var test = CreateTest(); + test.TestCode = """ + public class Test + { + public static event System.EventHandler {|MA0018:MyEvent|}; + } + """; + + return test.RunAsync(); + } + + [Fact] + public Task StaticMembers_EventWithExplicitAccessors() + { + var test = CreateTest(); + test.TestCode = """ + public class Test + { + public static event System.EventHandler {|MA0018:MyEvent|} + { + add => throw null; + remove => throw null; + } + } + """; + + return test.RunAsync(); + } + [Fact] public Task StaticMembers_Operator() {