Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Oct 26, 2025

MSTEST0037 was not reporting diagnostics for BCL types like TimeSpan and DateTime because their comparison operators are user-defined. The analyzer conservatively excluded all user-defined operators to avoid false positives.

Changes

  • Added IsBCLType helper - Identifies BCL types by comparing assembly public keys (same approach as existing IsBCLCollectionType)

  • Updated operator recognition methods to allow user-defined operators from BCL types:

    • RecognizeComparisonCheck - comparison operators (>, >=, <, <=)
    • RecognizeEqualityCheck - equality operators (==, !=)
    • IsEqualsNullBinaryOperator, IsNotEqualsNullBinaryOperator - null checks
    • RecognizeNullCheck - wrapper for null checks
  • Added tests for TimeSpan, DateTime, and user-defined types to prevent regressions

Example

Before:

var ts1 = TimeSpan.Zero;
var ts2 = TimeSpan.FromSeconds(1);
Assert.IsTrue(ts2 > ts1);  // No diagnostic

After:

Assert.IsTrue(ts2 > ts1);  // MSTEST0037: Use 'Assert.IsGreaterThan'

User-defined types remain unaffected:

Assert.IsTrue(myCustomType > anotherCustomType);  // No diagnostic (correct)

Fixes #3982

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • 7tjvsblobprodcus341.vsblob.vsassets.io
    • Triggering command: /home/REDACTED/work/testfx/testfx/.dotnet/dotnet msbuild /m /nologo /clp:Summary /v:minimal /nr:true /warnaserror /p:TreatWarningsAsErrors=true /p:ContinuousIntegrationBuild=false /home/REDACTED/work/testfx/testfx/artifacts/toolset/restore.proj /t:__WriteToolsetLocation /clp:ErrorsOnly;NoSummary /p:__ToolsetLocationOutputFile=/home/REDACTED/work/testfx/testfx/artifacts/toolset/11.0.0-beta.25524.1.txt (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Original prompt

This section details on the original issue you should resolve

<issue_title>MSTEST0037 not reported for (alll) types that implement IComparable</issue_title>
<issue_description>In MSTest v3.10.4, MSTEST0037 is currently not reported for all types that implement IComparable.

Steps To Reproduce

Compile the following code (with MSTest analyzers enabled):

using System;

using Microsoft.VisualStudio.TestTools.UnitTesting;

[TestClass]
public sealed class MSTEST0037Tests
{
    [TestMethod]
    public void TimeSpanComparison()
    {
        var ts1 = TimeSpan.Zero;
        var ts2 = TimeSpan.Zero;

        Assert.IsTrue(ts2 > ts1);
        Assert.IsTrue(ts2 >= ts1);
        Assert.IsTrue(ts2 < ts1);
        Assert.IsTrue(ts2 <= ts1);
        Assert.IsTrue(ts2 == ts1);
        Assert.IsTrue(ts2 != ts1);
    }

    [TestMethod]
    public void DateTimeComparison()
    {
        var ts1 = DateTime.Today;
        var ts2 = DateTime.Today;

        Assert.IsTrue(ts2 > ts1);
        Assert.IsTrue(ts2 >= ts1);
        Assert.IsTrue(ts2 < ts1);
        Assert.IsTrue(ts2 <= ts1);
        Assert.IsTrue(ts2 == ts1);
        Assert.IsTrue(ts2 != ts1);
    }
}

Expected behavior

MSTEST0037 is reported for each usage of Assert.IsTrue(...).

Actual behavior

MSTEST0037 is not reported at all.</issue_description>

<agent_instructions>Maybe what we can do here is exclude anything coming from the BCL from the user-defined operator check.</agent_instructions>

Comments on the Issue (you are @copilot in this section)

@Youssef1313 This is most likely because those operators are considered as user-defined operators, and we try to avoid false positives for user-defined operators. Maybe what we can do here is exclude anything coming from the BCL from the user-defined operator check. @Evangelink I think it would be valid for runtime team. We did the exclusion just as a safeguard because we don't know if people would implement comparison "correctly"

✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 2 commits October 26, 2025 11:20
- Add IsBCLType helper method to check if a type is from BCL by comparing assembly public key
- Update RecognizeComparisonCheck to allow user-defined operators from BCL types
- Update RecognizeEqualityCheck to allow user-defined operators from BCL types
- Update IsEqualsNullBinaryOperator and IsNotEqualsNullBinaryOperator for BCL types
- Add comprehensive tests for TimeSpan and DateTime comparisons

Co-authored-by: Evangelink <[email protected]>
Copilot AI changed the title [WIP] Fix MSTEST0037 not reported for IComparable<T> types Fix MSTEST0037: Report diagnostic for BCL types with user-defined operators Oct 26, 2025
Copilot AI requested a review from Evangelink October 26, 2025 11:29
Copilot finished work on behalf of Evangelink October 26, 2025 11:29
@Evangelink Evangelink marked this pull request as ready for review October 26, 2025 17:09
@Evangelink Evangelink enabled auto-merge (squash) October 27, 2025 05:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MSTEST0037 not reported for (alll) types that implement IComparable<T>

2 participants