Skip to content

Clarify the MA0095 comment about the Equals(object) lookup - #1518

Merged
meziantou merged 1 commit into
mainfrom
feature/ma0095-comment-clarification-7ce63e
Sep 12, 2026
Merged

meziantou merged 1 commit into
mainfrom
feature/ma0095-comment-clarification-7ce63e

Conversation

@meziantou

Copy link
Copy Markdown
Owner

What changed

The comment above the MA0095 check in EqualityShouldBeCorrectlyImplementedAnalyzer said:

Check the entire type hierarchy for an Equals(object) override

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 override Equals(object) itself, otherwise object.Equals (inherited from the base) and IEquatable<T>.Equals would 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

  • Comment-only change; no behavior change.
  • No new test needed: InheritedIEquatable_WithDirectImplementationToo_ShouldTrigger already locks in the scenario (base overrides Equals(object), derived implements IEquatable<Derived> → MA0095 reported).
  • Verified: Meziantou.Analyzer.roslyn5.9 builds with 0 warnings, the 46 EqualityShouldBeCorrectlyImplementedAnalyzerTests tests pass, and dotnet run --project src/DocumentationGenerator exits 0 with no markdown changes.

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.
@meziantou
meziantou merged commit f5547ed into main Sep 12, 2026
13 checks passed
@meziantou
meziantou deleted the feature/ma0095-comment-clarification-7ce63e branch September 12, 2026 20:18
This was referenced Sep 12, 2026
This was referenced Sep 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant