Repository navigation
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 intoJul 16, 2026
Conversation
…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
Amaury Levé (Evangelink)
approved these changes
Jul 16, 2026
Amaury Levé (Evangelink)
enabled auto-merge (squash)
July 16, 2026 11:24
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.
Backport of #9978 to rel/4.3
/cc Amaury Levé (@Evangelink)