Repository navigation
Don't flag Assert.AreEqual self-comparison for user-defined equality by @Evangelink in #10008 (backport to rel/4.3) - #10043
Merged
Amaury Levé (Evangelink) merged 9 commits intoJul 17, 2026
Conversation
…icrosoft#9972) MSTEST0032 (ReviewAlwaysTrueAssertConditionAnalyzer) and MSTEST0025 (PreferAssertFailOverAlwaysFalseConditionsAnalyzer) flagged Assert.AreEqual(x, x) / Assert.AreNotEqual(x, x) as always-true / always-false using a purely syntactic identical-argument check. Assert.AreEqual compares via EqualityComparer<T>.Default, which routes through user code when the compared type overrides object.Equals or implements IEquatable<T>. For those types, self-comparison is a legitimate reflexivity test and must not be reported. Add HasUserDefinedEquality and use it to guard the identical-argument path for AreEqual/AreNotEqual only. AreSame/AreNotSame keep pure reference-identity checks. Generic type parameters are treated conservatively. Co-authored-by: Copilot App <[email protected]> Copilot-Session: 7c0a4a79-474c-4ad1-a6e9-377508dd2b5d
… MSTEST0032/0025 - Skip the always-true/always-false determination for Assert.AreEqual/AreNotEqual overloads that take a caller-supplied IEqualityComparer<T>, since the comparer can return any result. - Only treat self-comparison as provably reflexive for value types, sealed types, and arrays; non-sealed reference types, interfaces, and type parameters can hold an instance whose overridden Equals is not reflexive. Co-authored-by: Copilot App <[email protected]> Copilot-Session: 7c0a4a79-474c-4ad1-a6e9-377508dd2b5d
…lue-type fields - Base the always-true/always-false proof on the type T of EqualityComparer<T>.Default that the invoked overload uses (the method type argument), not the operand's static type, which can differ through an implicit conversion. - Only prove reflexivity for primitives, enums, arrays, and sealed reference types without custom equality. Non-primitive structs (default field-based ValueType.Equals) and Nullable<T> are treated conservatively since a field/underlying type could be non-reflexive. Co-authored-by: Copilot App <[email protected]> Copilot-Session: 7c0a4a79-474c-4ad1-a6e9-377508dd2b5d
…ion branch Use a sealed class implementing IEquatable<self> (without overriding object.Equals) so the test fails if the interface-detection branch regresses; a non-special struct is short-circuited before HasUserDefinedEquality is reached. Co-authored-by: Copilot App <[email protected]> Copilot-Session: 7c0a4a79-474c-4ad1-a6e9-377508dd2b5d
… is null MSTest treats a null IEqualityComparer<T> as EqualityComparer<T>.Default, so AreEqual(x, x, null) / AreNotEqual(x, x, null) remain provably always-true/false for reflexive types. Refined the guard from HasEqualityComparerParameter (any comparer overload) to HasNonDefaultEqualityComparerArgument (skips only when the comparer argument is non-null / non-constant). Added null-comparer regression tests for both analyzers. Co-authored-by: Copilot App <[email protected]> Copilot-Session: 7c0a4a79-474c-4ad1-a6e9-377508dd2b5d
Use GetMembers(nameof(object.Equals)) instead of enumerating all members on each base type, avoiding avoidable overhead in the analyzer. No behavior change. Co-authored-by: Copilot App <[email protected]> Copilot-Session: 7c0a4a79-474c-4ad1-a6e9-377508dd2b5d
…rer argument GetArgumentWithName uses WalkDownConversion, which strips user-defined conversions too. Use the raw argument with WalkDownBuiltInConversion so a conversion cannot hide a null source; check the resulting constant. Added a default(IEqualityComparer<T>) regression test. Co-authored-by: Copilot App <[email protected]> Copilot-Session: 7c0a4a79-474c-4ad1-a6e9-377508dd2b5d
…ault comparer Passing EqualityComparer<T>.Default explicitly is semantically identical to passing null/omitting the comparer, so it should not suppress MSTEST0032/MSTEST0025. Recognize the EqualityComparer<T>.Default property reference alongside a null argument. Added regression tests for both analyzers. Co-authored-by: Copilot App <[email protected]> Copilot-Session: 7c0a4a79-474c-4ad1-a6e9-377508dd2b5d
…ault and restore BOM - EqualityComparer<X>.Default is only the default comparer when X matches the comparer parameter's type argument. IEqualityComparer<T> is contravariant, so EqualityComparer<Base>.Default can be passed as IEqualityComparer<Derived> and is a non-default comparer. Added a contravariant regression test. - Restore the UTF-8 BOM on AssertConditionAnalyzerHelper.cs to satisfy .editorconfig (charset = utf-8-bom). Co-authored-by: Copilot App <[email protected]> Copilot-Session: 7c0a4a79-474c-4ad1-a6e9-377508dd2b5d
Amaury Levé (Evangelink)
enabled auto-merge (squash)
July 17, 2026 18:04
Amaury Levé (Evangelink)
approved these changes
Jul 17, 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.
Backport of #10008 to rel/4.3
/cc Amaury Levé (@Evangelink)