Skip to content

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 into
microsoft:rel/4.3from
nohwnd-bot:backport/10008/to/rel/4.3
Jul 17, 2026
Merged

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

Conversation

@nohwnd-bot

Copy link
Copy Markdown
Contributor

Backport of #10008 to rel/4.3

/cc Amaury Levé (@Evangelink)

…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
@Evangelink Amaury Levé (Evangelink) added the state/needs-review Awaiting review from the team. label Jul 17, 2026
@Evangelink
Amaury Levé (Evangelink) enabled auto-merge (squash) July 17, 2026 18:04
@Evangelink
Amaury Levé (Evangelink) merged commit 826e8c2 into microsoft:rel/4.3 Jul 17, 2026
32 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

state/needs-review Awaiting review from the team.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants