Skip to content

Don't report MSTEST0065 on collection types that declare their own equality by @Evangelink in #9978 (backport to rel/4.3) - #10003

Merged
Amaury Levé (Evangelink) merged 8 commits into
microsoft:rel/4.3from
nohwnd-bot:backport/9978/to/rel/4.3
Jul 16, 2026
Merged

Amaury Levé (Evangelink) merged 8 commits into
microsoft:rel/4.3from
nohwnd-bot:backport/9978/to/rel/4.3

Conversation

@nohwnd-bot

Copy link
Copy Markdown
Contributor

Backport of #9978 to rel/4.3

/cc Amaury Levé (@Evangelink)

…uality

Suppress MSTEST0065 when the compared type implements IEquatable<itself>
or overrides object.Equals. In that case the author has deliberately
opted into a custom, non-reference equality that Assert.AreEqual honors
via EqualityComparer<T>.Default, so suggesting a sequence/structural
comparison second-guesses a deliberate decision.

Fixes microsoft#9971

Co-authored-by: Copilot App <[email protected]>
Copilot-Session: 78d66f93-e4cf-45d3-bdde-07ee9f67833d
…onstraints, override root

- Base the IEquatable/Equals opt-out on the selected generic type argument T
  (the equality contract EqualityComparer<T>.Default actually uses), so widening
  to a base type (AreEqual<object>) no longer loses the diagnostic.
- Skip the opt-out when an explicit comparer argument is supplied.
- Honor IEquatable<T> (and self-equating class) type-parameter constraints.
- Follow the override chain to its root and require object.Equals, so a derived
  override of a base 'new virtual bool Equals(object)' is not mistaken for one.

Co-authored-by: Copilot App <[email protected]>
Copilot-Session: 78d66f93-e4cf-45d3-bdde-07ee9f67833d
…quals walk

- Type-parameter constraints: keep T as the equality target. A where T : ISelf
  constraint where ISelf implements IEquatable<ISelf> no longer suppresses, since a
  concrete T need not implement IEquatable<T>. Only IEquatable<T> (self) constraints
  or class constraints overriding object.Equals suppress.
- OverridesObjectEquals now stops before System.ValueType/System.Enum, so a struct
  collection with no equality of its own is still reported (ValueType.Equals is not
  the author's intentional equality).

Co-authored-by: Copilot App <[email protected]>
Copilot-Session: 78d66f93-e4cf-45d3-bdde-07ee9f67833d
…dd coverage

- Document that transitive type-parameter constraints are intentionally not followed.
- Clarify the equatableSymbol==null comment (only IEquatable<self> becomes a no-op).
- Add tests: explicit-interface IEquatable implementation (suppresses) and
  AreNotEqual widened-to-object (reports).

Co-authored-by: Copilot App <[email protected]>
Copilot-Session: 78d66f93-e4cf-45d3-bdde-07ee9f67833d
…traints, comment fixes

- HasCustomComparerArgument: a null-literal or explicit EqualityComparer<T>.Default
  comparer now keeps the equality opt-out (both dispatch to the default comparer per
  Assert.AreEqual.cs), only a genuinely custom comparer re-enables the diagnostic.
- Type-parameter constraints are now traversed transitively (where T : U where U : ...),
  keeping T as the IEquatable equality target while honoring inherited object.Equals.
- Reworded the struct fixture comment (no backing field) to describe ValueType's
  reflection-based field-wise equality accurately.
- Added tests: custom comparer (reports), null/default comparer (suppress), transitive
  class constraint (suppress), IEquatable<Base>-only used as Derived (reports, since
  IEquatable<T> is invariant).

Co-authored-by: Copilot App <[email protected]>
Copilot-Session: 78d66f93-e4cf-45d3-bdde-07ee9f67833d
… as the default

HasCustomComparerArgument now uses IOperation.ConstantValue instead of matching only
a null literal, so default, default(IEqualityComparer<T>), and const null fields are all
recognized as the default comparer and keep the equality opt-out. Added a default(...)
comparer test.

Co-authored-by: Copilot App <[email protected]>
Copilot-Session: 78d66f93-e4cf-45d3-bdde-07ee9f67833d
Peel only built-in conversions (not user-defined) when classifying the comparer
argument, matching GetCollectionArgumentType and the runtime value. A user-defined
conversion executes and can change the value, so it must stay opaque.

Co-authored-by: Copilot App <[email protected]>
Copilot-Session: 78d66f93-e4cf-45d3-bdde-07ee9f67833d
…ected T

IEqualityComparer<in T> is contravariant, so EqualityComparer<Base>.Default compiles as
the comparer for a Derived argument but dispatches to Base's equality. Require the
property's type argument to equal the selected T so such a base comparer is treated as
custom and MSTEST0065 still fires. Added a variance regression test.

Co-authored-by: Copilot App <[email protected]>
Copilot-Session: 78d66f93-e4cf-45d3-bdde-07ee9f67833d
@Evangelink
Amaury Levé (Evangelink) enabled auto-merge (squash) July 16, 2026 11:24
@Evangelink
Amaury Levé (Evangelink) merged commit be18ca1 into microsoft:rel/4.3 Jul 16, 2026
11 checks passed
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.

2 participants