Skip to content

Fix MSTEST0037 false positive for non-int count argument#7624

Merged
Evangelink merged 3 commits intomainfrom
copilot/fix-mstest0037-nullable-args-handling
Mar 30, 2026
Merged

Fix MSTEST0037 false positive for non-int count argument#7624
Evangelink merged 3 commits intomainfrom
copilot/fix-mstest0037-nullable-args-handling

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 28, 2026

Assert.HasCount(int expected, ...) only accepts int, so MSTEST0037 must not suggest replacing Assert.AreEqual(expected, collection.Count) with Assert.HasCount(expected, collection) when the expected argument is not int — doing so produces a CS1503 compile error.

Changes

  • UseProperAssertMethodsAnalyzer.cs — In RecognizeCountCheck, return CountCheckStatus.Unknown when expectedArgument.Type?.SpecialType != SpecialType.System_Int32 in the HasCount branch. This covers all non-int types including int?, long, uint, decimal, etc. 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 WhenAssertAreEqualWithCollectionCountNonZeroNonIntExpected verifying no diagnostic is raised for both patterns when the expected value is int? or long.

Example

int? nullableCount = 3;
long longCount = 3L;

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

💬 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
@Evangelink Evangelink marked this pull request as ready for review March 30, 2026 07:30
Copilot AI review requested due to automatic review settings March 30, 2026 07:30
@Evangelink Evangelink enabled auto-merge March 30, 2026 07:30
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Fixes MSTEST0037 (UseProperAssertMethodsAnalyzer) incorrectly suggesting Assert.HasCount(expected, collection) when the expected count is a nullable value type (e.g., int?), which would otherwise lead to a CS1503 compile error after applying the code fix.

Changes:

  • Update RecognizeCountCheck to return CountCheckStatus.Unknown for the HasCount suggestion when expectedArgument.Type is a nullable value type, for both .Count/.Length and LINQ .Count() patterns.
  • Add a unit test ensuring no MSTEST0037 diagnostic is produced for Assert.AreEqual(int?, collection.Count) and Assert.AreEqual(int?, enumerable.Count()).

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/Analyzers/MSTest.Analyzers/UseProperAssertMethodsAnalyzer.cs Suppresses HasCount suggestion when the expected argument is nullable to avoid non-compiling fixes.
test/UnitTests/MSTest.Analyzers.UnitTests/UseProperAssertMethodsAnalyzerTests.cs Adds coverage for nullable expected count (both property and LINQ Count patterns).
Comments suppressed due to low confidence (1)

src/Analyzers/MSTest.Analyzers/UseProperAssertMethodsAnalyzer.cs:1482

  • Same as above for the LINQ Count() pattern: returning HasCount without validating that expectedArgument.Type can be passed to Assert.HasCount(int, ...) can lead to diagnostics/code fixes that don't compile when expected is a non-int numeric type. Suggest checking implicit conversion to int (or restricting to appropriate integral types) before returning HasCount.
                // Assert.HasCount takes int, not int?, so skip if expectedCount is a nullable value type.
                if (expectedArgument.Type.IsNullableValueType())
                {
                    nodeToBeReplaced1 = null;
                    replacement1 = null;
                    nodeToBeReplaced2 = null;
                    replacement2 = null;
                    return CountCheckStatus.Unknown;
                }

Comment thread src/Analyzers/MSTest.Analyzers/UseProperAssertMethodsAnalyzer.cs Outdated
Comment thread src/Analyzers/MSTest.Analyzers/UseProperAssertMethodsAnalyzer.cs Outdated
Comment thread src/Analyzers/MSTest.Analyzers/UseProperAssertMethodsAnalyzer.cs Outdated
Copilot AI changed the title Fix MSTEST0037 false positive for nullable count argument Fix MSTEST0037 false positive for non-int count argument Mar 30, 2026
Copilot AI requested a review from Evangelink March 30, 2026 08:25
@Evangelink Evangelink merged commit 48ece15 into main Mar 30, 2026
10 checks passed
@Evangelink Evangelink deleted the copilot/fix-mstest0037-nullable-args-handling branch March 30, 2026 12:37
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

5 participants