Skip to content

Don't flag Assert.AreEqual self-comparison for user-defined equality - #10008

Merged
Amaury Levé (Evangelink) merged 9 commits into
mainfrom
dev/amauryleve/upgraded-guacamole
Jul 16, 2026
Merged

Amaury Levé (Evangelink) merged 9 commits into
mainfrom
dev/amauryleve/upgraded-guacamole

Conversation

@Evangelink

Copy link
Copy Markdown
Member

Fixes #9972

Problem

MSTEST0032 (ReviewAlwaysTrueAssertConditionAnalyzer) and MSTEST0025 (PreferAssertFailOverAlwaysFalseConditionsAnalyzer) flagged Assert.AreEqual(x, x) / Assert.AreNotEqual(x, x) as always-true / always-false based on a purely syntactic identical-argument check.

But Assert.AreEqual compares via EqualityComparer<T>.Default, which routes through user code when the compared type overrides object.Equals or implements IEquatable<T>. For such types, self-comparison is a legitimate way to exercise the equality contract (reflexivity) and might not evaluate to a constant, so flagging it is a false positive — exactly the scenario reported in #9972.

Fix

  • Added HasUserDefinedEquality(ITypeSymbol?) and a guarded HasIdenticalExpectedAndActualWithBuiltInEquality helper.
  • AreEqual (MSTEST0032) and AreNotEqual (MSTEST0025) now use the guarded check, so self-comparison is only reported when equality is not user-defined.
  • AreSame / AreNotSame are unchanged — reference identity is genuinely reflexive and stays flagged.

Detection semantics

Equality detection matches how EqualityComparer<T>.Default actually dispatches at runtime:

  • an object.Equals(object) override (walking base types), or
  • IEquatable<T> implemented for the compared type itself (IEquatable<SomethingElse> correctly still gets flagged).

Notably, operator == is not consulted, because EqualityComparer<T>.Default never calls it. Generic type parameters are treated conservatively (never flagged) since they can be substituted with a non-reflexive type.

Behavior preserved

Self-comparison of primitives, enums, and reference types without custom equality is still flagged.

Tests

Added coverage to both analyzer test classes: overridden Equals, IEquatable<self>, IEquatable<other> (still flagged), ==-only (still flagged), generic type parameter (not flagged), and no-custom-equality (still flagged). All 149 tests in the two analyzer test classes pass.

No public API, resource (.resx/.xlf), or documentation changes required (the new helper is internal; no new diagnostic ID).

…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
Copilot AI review requested due to automatic review settings July 16, 2026 12:17

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Prevents false-positive self-comparison diagnostics when equality can invoke user-defined code.

Changes:

  • Detects overridden Equals and matching IEquatable<T>.
  • Applies guarded checks to AreEqual and AreNotEqual.
  • Adds analyzer regression tests.
Show a summary per file
File Description
AssertConditionAnalyzerHelper.cs Adds user-defined equality detection.
ReviewAlwaysTrueAssertConditionAnalyzer.cs Guards AreEqual diagnostics.
PreferAssertFailOverAlwaysFalseConditionsAnalyzer.cs Guards AreNotEqual diagnostics.
ReviewAlwaysTrueAssertConditionAnalyzerTests.cs Adds MSTEST0032 coverage.
PreferAssertFailOverAlwaysFalseConditionsAnalyzerTests.cs Adds MSTEST0025 coverage.

Review details

  • Files reviewed: 5/5 changed files
  • Comments generated: 2
  • Review effort level: Medium

@github-actions

This comment has been minimized.

@Evangelink
Amaury Levé (Evangelink) enabled auto-merge (squash) July 16, 2026 13:05
… 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
Copilot AI review requested due to automatic review settings July 16, 2026 13:20

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review details

  • Files reviewed: 5/5 changed files
  • Comments generated: 2
  • Review effort level: Medium

@github-actions

This comment has been minimized.

…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
Copilot AI review requested due to automatic review settings July 16, 2026 13:57

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review details

  • Files reviewed: 5/5 changed files
  • Comments generated: 1
  • Review effort level: Medium

…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
Copilot AI review requested due to automatic review settings July 16, 2026 14:05

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review details

  • Files reviewed: 5/5 changed files
  • Comments generated: 2
  • Review effort level: Low

… 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
Copilot AI review requested due to automatic review settings July 16, 2026 14:15

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review details

  • Files reviewed: 5/5 changed files
  • Comments generated: 1
  • Review effort level: Low

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
Copilot AI review requested due to automatic review settings July 16, 2026 14:21

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review details

  • Files reviewed: 5/5 changed files
  • Comments generated: 1
  • Review effort level: Medium

…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
Copilot AI review requested due to automatic review settings July 16, 2026 14:32

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review details

  • Files reviewed: 5/5 changed files
  • Comments generated: 1
  • Review effort level: Low

…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
Copilot AI review requested due to automatic review settings July 16, 2026 14:42

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review details

  • Files reviewed: 5/5 changed files
  • Comments generated: 2
  • Review effort level: Medium

…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
Copilot AI review requested due to automatic review settings July 16, 2026 14:53

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review details

  • Files reviewed: 5/5 changed files
  • Comments generated: 0 new
  • Review effort level: Medium

@github-actions

Copy link
Copy Markdown
Contributor

🧪 Test quality grade — PR #10008

GradeTestNotes
A (90–100) new PreferAssertFailOverAlwaysFalseConditionsAnalyzerTests.
WhenAssertAreEqualIsPassedNonEqualConstantsWithCustomComparer_
NoDiagnostic
Explains suppression with custom comparer; VerifyCodeFixAsync verifies no diagnostic fires and code is unchanged. No issues found.
A (90–100) new PreferAssertFailOverAlwaysFalseConditionsAnalyzerTests.
WhenAssertAreNotEqualIsPassedSameLocalWithCustomComparer_
NoDiagnostic
Custom comparer negates self-comparison predictability; correctly suppressed. No issues found.
A (90–100) new PreferAssertFailOverAlwaysFalseConditionsAnalyzerTests.
WhenAssertAreNotEqualIsPassedSameLocalWithDefaultEqualityComparer_
Diagnostic
Comment explains EqualityComparer(T).Default = default; diagnostic fires and fix removes the call. No issues found.
A (90–100) new PreferAssertFailOverAlwaysFalseConditionsAnalyzerTests.
WhenAssertAreNotEqualIsPassedSameLocalWithNullComparer_
Diagnostic
Comment explains null-comparer semantics; diagnostic fires at the right location and fix is correct. No issues found.
A (90–100) new PreferAssertFailOverAlwaysFalseConditionsAnalyzerTests.
WhenAssertAreNotEqualIsPassedSameLocalWithoutCustomEquality_
Diagnostic
No custom equality means self-comparison is always false; diagnostic placement and fix both verified. No issues found.
A (90–100) new PreferAssertFailOverAlwaysFalseConditionsAnalyzerTests.
WhenAssertAreNotEqualIsPassedSameLocalWithOverriddenEquals_
NoDiagnostic
Comment cites issue #9972; correctly suppresses diagnostic when Equals is overridden. No issues found.
A (90–100) new ReviewAlwaysTrueAssertConditionAnalyzerTests.
WhenAssertAreEqualIsPassedContravariantDefaultComparer_
NoDiagnostic
Nuanced IEqualityComparer contravariance scenario; comment explains the distinction precisely. No issues found.
A (90–100) new ReviewAlwaysTrueAssertConditionAnalyzerTests.
WhenAssertAreEqualIsPassedEqualConstantsWithCustomComparer_
NoDiagnostic
Custom comparer can return any result; no diagnostic is correct. No issues found.
A (90–100) new ReviewAlwaysTrueAssertConditionAnalyzerTests.
WhenAssertAreEqualIsPassedSameArray_
Diagnostic
Arrays use reference equality; self-comparison is genuinely always true. Comment and diagnostic correct. No issues found.
A (90–100) new ReviewAlwaysTrueAssertConditionAnalyzerTests.
WhenAssertAreEqualIsPassedSameGenericTypeParameter_
NoDiagnostic
Generic T may have non-reflexive equality; correct suppression. No issues found.
A (90–100) new ReviewAlwaysTrueAssertConditionAnalyzerTests.
WhenAssertAreEqualIsPassedSameLocalOfNonSealedType_
NoDiagnostic
Non-sealed type may hold a derived instance with non-reflexive Equals; suppression is correct. No issues found.
A (90–100) new ReviewAlwaysTrueAssertConditionAnalyzerTests.
WhenAssertAreEqualIsPassedSameLocalOfPolymorphicType_
NoDiagnostic
Static type object can hold derived instances; conservative suppression is correct. No issues found.
A (90–100) new ReviewAlwaysTrueAssertConditionAnalyzerTests.
WhenAssertAreEqualIsPassedSameLocalWithCustomComparer_
NoDiagnostic
Custom NeverEqualComparer prevents the self-comparison from being always true; no diagnostic. No issues found.
A (90–100) new ReviewAlwaysTrueAssertConditionAnalyzerTests.
WhenAssertAreEqualIsPassedSameLocalWithDefaultComparer_
Diagnostic
Explains default(IEqualityComparer(T)) as null; diagnostic fires correctly. No issues found.
A (90–100) new ReviewAlwaysTrueAssertConditionAnalyzerTests.
WhenAssertAreEqualIsPassedSameLocalWithDefaultEqualityComparer_
Diagnostic
Explicit EqualityComparer(T).Default is still the default; diagnostic fires correctly. No issues found.
A (90–100) new ReviewAlwaysTrueAssertConditionAnalyzerTests.
WhenAssertAreEqualIsPassedSameLocalWithEquatable_
NoDiagnostic
IEquatable(self) detection branch exercised; correctly suppressed. No issues found.
A (90–100) new ReviewAlwaysTrueAssertConditionAnalyzerTests.
WhenAssertAreEqualIsPassedSameLocalWithEquatableOfOtherType_
Diagnostic
IEquatable(string) not IEquatable(MyType); reference equality applies and diagnostic fires. No issues found.
A (90–100) new ReviewAlwaysTrueAssertConditionAnalyzerTests.
WhenAssertAreEqualIsPassedSameLocalWithNullComparer_
Diagnostic
Null comparer treated as default; diagnostic fires at correct location. No issues found.
A (90–100) new ReviewAlwaysTrueAssertConditionAnalyzerTests.
WhenAssertAreEqualIsPassedSameLocalWithOnlyEqualityOperator_
Diagnostic
Comment explains EqualityComparer never calls operator ==; diagnostic fires correctly. No issues found.
A (90–100) new ReviewAlwaysTrueAssertConditionAnalyzerTests.
WhenAssertAreEqualIsPassedSameLocalWithoutCustomEquality_
Diagnostic
Plain reference type with no Equals override; self-comparison always true and diagnostic fires. No issues found.
A (90–100) new ReviewAlwaysTrueAssertConditionAnalyzerTests.
WhenAssertAreEqualIsPassedSameLocalWithOverriddenEquals_
NoDiagnostic
Routes through user Equals; correctly suppressed per issue #9972 semantics. No issues found.
A (90–100) new ReviewAlwaysTrueAssertConditionAnalyzerTests.
WhenAssertAreEqualIsPassedSameNullable_
NoDiagnostic
Nullable(T) delegates to T's equality which may not be reflexive; conservative suppression. No issues found.
A (90–100) new ReviewAlwaysTrueAssertConditionAnalyzerTests.
WhenAssertAreEqualIsPassedSameStructWithNonReflexiveField_
NoDiagnostic
ValueType.Equals uses field equality; struct with non-reflexive field cannot be flagged. No issues found.
A (90–100) new ReviewAlwaysTrueAssertConditionAnalyzerTests.
WhenAssertAreEqualIsPassedSameValueWithPolymorphicComparerType_
NoDiagnostic
Type argument Base routes through non-reflexive IEquatable(Base) even for a Derived operand. No issues found.

This advisory comment was generated automatically. Grades are heuristic
and informational — they do not block merging. Re-run with
/grade-tests.

Warning

Firewall blocked 1 domain

The following domain was blocked by the firewall during workflow execution:

  • awmgmcpg

To allow these domains, add them to the network.allowed list in your workflow frontmatter:

network:
  allowed:
    - defaults
    - "awmgmcpg"

See Network Configuration for more information.

🤖 Automated content by GitHub Copilot. Generated by the Grade Tests on PR (on open / sync) workflow. · 85.3 AIC · ⌖ 7.01 AIC · ⊞ 8.9K · [◷]( · ◷)

@Evangelink Amaury Levé (Evangelink) added the state/needs-review Awaiting review from the team. label Jul 16, 2026
@Evangelink
Amaury Levé (Evangelink) merged commit 5c34e7f into main Jul 16, 2026
52 checks passed
@Evangelink
Amaury Levé (Evangelink) deleted the dev/amauryleve/upgraded-guacamole branch July 16, 2026 18:06
@Evangelink

Copy link
Copy Markdown
Member Author

/backport to rel/4.3

@github-actions

Copy link
Copy Markdown
Contributor

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.

Provide an Assert overload to explicitly check self equality

3 participants