Clarify the MA0095 comment about the Equals(object) lookup - #1518
Merged
Merged
Conversation
The comment claimed the entire type hierarchy was checked for an Equals(object) override, but the code only looks at the members declared by the type itself. The behavior is intentional and documented: a type that introduces new equality semantics through IEquatable<T> must override Equals(object) itself, otherwise the inherited override would disagree with them. Only the comment was wrong, so it now describes what the code does and why.
This was referenced Sep 12, 2026
Closed
This was referenced Sep 26, 2026
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
The comment above the MA0095 check in
EqualityShouldBeCorrectlyImplementedAnalyzersaid:but the code calls
symbol.GetMembers(), which only returns the members declared by the type itself — a base-type override is not visible there. The comment now states what the code does and why.Why
The behavior is correct and intentional: a type that introduces new equality semantics through
IEquatable<T>must overrideEquals(object)itself, otherwiseobject.Equals(inherited from the base) andIEquatable<T>.Equalswould disagree. This exact shape is listed as non-compliant in docs/Rules/MA0095.md. Only the comment was misleading — it suggested a bug where there is none, and could prompt a "fix" that breaks the rule.Notes for reviewers
InheritedIEquatable_WithDirectImplementationToo_ShouldTriggeralready locks in the scenario (base overridesEquals(object), derived implementsIEquatable<Derived>→ MA0095 reported).Meziantou.Analyzer.roslyn5.9builds with 0 warnings, the 46EqualityShouldBeCorrectlyImplementedAnalyzerTeststests pass, anddotnet run --project src/DocumentationGeneratorexits 0 with no markdown changes.