Repository navigation
Don't flag Assert.AreEqual self-comparison for user-defined equality - #10008
Conversation
…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
There was a problem hiding this comment.
Pull request overview
Prevents false-positive self-comparison diagnostics when equality can invoke user-defined code.
Changes:
- Detects overridden
Equalsand matchingIEquatable<T>. - Applies guarded checks to
AreEqualandAreNotEqual. - 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
This comment has been minimized.
This comment has been minimized.
… 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
This comment has been minimized.
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
…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
🧪 Test quality grade — PR #10008
This advisory comment was generated automatically. Grades are heuristic Warning Firewall blocked 1 domainThe following domain was blocked by the firewall during workflow execution:
network:
allowed:
- defaults
- "awmgmcpg"See Network Configuration for more information.
|
|
/backport to rel/4.3 |
|
Started backporting to rel/4.3: https://github.com/microsoft/testfx/actions/runs/29600175733 |
@Evangelink in #10008 (backport to rel/4.3) (#10043) Co-authored-by: Amaury Levé <[email protected]>
Fixes #9972
Problem
MSTEST0032(ReviewAlwaysTrueAssertConditionAnalyzer) andMSTEST0025(PreferAssertFailOverAlwaysFalseConditionsAnalyzer) flaggedAssert.AreEqual(x, x)/Assert.AreNotEqual(x, x)as always-true / always-false based on a purely syntactic identical-argument check.But
Assert.AreEqualcompares viaEqualityComparer<T>.Default, which routes through user code when the compared type overridesobject.Equalsor implementsIEquatable<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
HasUserDefinedEquality(ITypeSymbol?)and a guardedHasIdenticalExpectedAndActualWithBuiltInEqualityhelper.AreEqual(MSTEST0032) andAreNotEqual(MSTEST0025) now use the guarded check, so self-comparison is only reported when equality is not user-defined.AreSame/AreNotSameare unchanged — reference identity is genuinely reflexive and stays flagged.Detection semantics
Equality detection matches how
EqualityComparer<T>.Defaultactually dispatches at runtime:object.Equals(object)override (walking base types), orIEquatable<T>implemented for the compared type itself (IEquatable<SomethingElse>correctly still gets flagged).Notably,
operator ==is not consulted, becauseEqualityComparer<T>.Defaultnever 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 isinternal; no new diagnostic ID).