Fix MA0018 reporting one diagnostic per event accessor - #1505
Merged
Merged
Conversation
`Analyze` skipped `PropertyGet` and `PropertySet` accessors so that a static property is reported once, through its `IPropertySymbol`, but the event accessor kinds were missing from that filter. For a static event, `GetMembers()` returns the `IEventSymbol` and its `add_`/`remove_` accessors, all three public and static, so all three were reported. With explicitly written accessors the three diagnostics land on three different spans, so the IDE does not even deduplicate them. Add `EventAdd` and `EventRemove` to the skip list. `IsImplicitlyDeclared` would not work here: accessors written explicitly are not implicit.
This was referenced Sep 12, 2026
Closed
This was referenced Sep 24, 2026
Open
Open
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What changed
DoNotDeclareStaticMembersOnGenericTypes(MA0018) skippedPropertyGetandPropertySetaccessors so that a static property is reported once, through itsIPropertySymbol. The event accessor kinds were missing from that filter, soMethodKind.EventAddandMethodKind.EventRemoveare now skipped too. The comment, which said "skip properties" while the code skips accessors, was corrected.Why
For a static event,
GetMembers()returns theIEventSymboland itsadd_/remove_accessors, all three public and static, so all three were reported:produced three identical diagnostics at the same location:
With explicitly written
add/removeaccessors the three diagnostics land on three different spans, so the IDE does not even deduplicate them.Notes for reviewers
member.IsImplicitlyDeclaredinstead would not be enough: accessors written explicitly are not implicit.IsVisibleOutsideOfAssembly()already excluded it — it never contributed a diagnostic.DoNotDeclareStaticMembersOnGenericTypesTests: a field-like static event and one with explicitadd/removeaccessors (the case with distinct spans). Temporarily reverting the analyzer change makes exactly those two fail, so they do pin the defect.dotnet run --project src/DocumentationGeneratorexits 0 with no markdown changes.docs/Rules/MA0018.mdneeds no update: the documented behavior is unchanged, this only removes duplicates.