Skip to content

Fix MSTEST0037 false positive for nullable count argument#7624

Draft
Copilot wants to merge 2 commits intomainfrom
copilot/fix-mstest0037-nullable-args-handling
Draft

Fix MSTEST0037 false positive for nullable count argument#7624
Copilot wants to merge 2 commits intomainfrom
copilot/fix-mstest0037-nullable-args-handling

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 28, 2026

Assert.HasCount(int expected, ...) does not accept int?, so MSTEST0037 must not suggest replacing Assert.AreEqual(nullableCount, collection.Count) with Assert.HasCount(nullableCount, collection) — doing so produces a CS1503 compile error.

Changes

  • UseProperAssertMethodsAnalyzer.cs — In RecognizeCountCheck, return CountCheckStatus.Unknown when expectedArgument.Type is a nullable value type in the HasCount branch. Applies to both the BCL .Count/.Length property pattern and the LINQ .Count() method pattern. The IsEmpty branch (expected == 0) is unaffected since the expected argument is removed entirely by the fix.
  • UseProperAssertMethodsAnalyzerTests.cs — Add WhenAssertAreEqualWithCollectionCountNonZeroNullableExpected verifying no diagnostic is raised for both patterns when the expected value is int?.

Example

int? count = 3;

// Before: MSTEST0037 fired, fixer produced Assert.HasCount(count, list) → CS1503
// After: no diagnostic
Assert.AreEqual(count, list.Count);
Assert.AreEqual(count, list.AsEnumerable().Count());

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:

  • x3yvsblobprodcus370.vsblob.vsassets.io
    • Triggering command: /home/REDACTED/work/testfx/testfx/artifacts/bin/MSTest.Analyzers.UnitTests/Debug/net8.0/MSTest.Analyzers.UnitTests /home/REDACTED/work/testfx/testfx/artifacts/bin/MSTest.Analyzers.UnitTests/Debug/net8.0/MSTest.Analyzers.UnitTests --diagnostic --diagnostic-output-directory /home/REDACTED/work/testfx/testfx/artifacts/log/Debug --diagnostic-file-prefix MSTest.Analyzers.UnitTests_net8.0_Debug_x64 --diagnostic-verbosity trace --crashdump --hangdump --hangdump-timeout 15m --report-azdo --coverage --coverage-settings /home/REDACTED/work/testfx/testfx/test/coverage.config --coverage-output MSTest.Analyzers.UnitTests_net8.0_Debug_x64.coverage --filter WhenAssertAreEqualWithCollectionCountNonZero (dns block)
    • Triggering command: /home/REDACTED/work/testfx/testfx/artifacts/bin/MSTest.Analyzers.UnitTests/Debug/net8.0/MSTest.Analyzers.UnitTests /home/REDACTED/work/testfx/testfx/artifacts/bin/MSTest.Analyzers.UnitTests/Debug/net8.0/MSTest.Analyzers.UnitTests --diagnostic --diagnostic-output-directory /home/REDACTED/work/testfx/testfx/artifacts/log/Debug --diagnostic-file-prefix MSTest.Analyzers.UnitTests_net8.0_Debug_x64 --diagnostic-verbosity trace --crashdump --hangdump --hangdump-timeout 15m --report-azdo --coverage --coverage-settings /home/REDACTED/work/testfx/testfx/test/coverage.config --coverage-output MSTest.Analyzers.UnitTests_net8.0_Debug_x64.coverage --filter WhenAssertAreEqualWithCollectionCountZero (dns block)

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


💬 Send tasks to Copilot coding agent from Slack and Teams to turn conversations into code. Copilot posts an update in your thread when it's finished.

Copilot AI changed the title [WIP] Fix MSTEST0037 nullable arguments handling in fixer Fix MSTEST0037 false positive for nullable count argument Mar 28, 2026
Copilot AI requested a review from Evangelink March 28, 2026 06:46
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 does not handle nullable arguments correctly

2 participants