Fix MSTEST0037 false positive for non-int count argument#7624
Merged
Evangelink merged 3 commits intomainfrom Mar 30, 2026
Merged
Fix MSTEST0037 false positive for non-int count argument#7624Evangelink merged 3 commits intomainfrom
Evangelink merged 3 commits intomainfrom
Conversation
…uments Agent-Logs-Url: https://github.com/microsoft/testfx/sessions/cfe39d4b-438c-4b84-b10a-cc44fed45592 Co-authored-by: Evangelink <11340282+Evangelink@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix MSTEST0037 nullable arguments handling in fixer
Fix MSTEST0037 false positive for nullable count argument
Mar 28, 2026
Evangelink
approved these changes
Mar 30, 2026
Contributor
There was a problem hiding this comment.
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
RecognizeCountCheckto returnCountCheckStatus.Unknownfor theHasCountsuggestion whenexpectedArgument.Typeis a nullable value type, for both.Count/.Lengthand LINQ.Count()patterns. - Add a unit test ensuring no MSTEST0037 diagnostic is produced for
Assert.AreEqual(int?, collection.Count)andAssert.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: returningHasCountwithout validating thatexpectedArgument.Typecan be passed toAssert.HasCount(int, ...)can lead to diagnostics/code fixes that don't compile whenexpectedis a non-intnumeric type. Suggest checking implicit conversion toint(or restricting to appropriate integral types) before returningHasCount.
// 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;
}
Youssef1313
reviewed
Mar 30, 2026
Youssef1313
reviewed
Mar 30, 2026
…ed argument Agent-Logs-Url: https://github.com/microsoft/testfx/sessions/7a39bf37-3ac8-42ed-8a5a-07c0c2c5e22a Co-authored-by: Evangelink <11340282+Evangelink@users.noreply.github.com>
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
Evangelink
approved these changes
Mar 30, 2026
YuliiaKovalova
approved these changes
Mar 30, 2026
This was referenced Mar 30, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Assert.HasCount(int expected, ...)only acceptsint, so MSTEST0037 must not suggest replacingAssert.AreEqual(expected, collection.Count)withAssert.HasCount(expected, collection)when the expected argument is notint— doing so produces aCS1503compile error.Changes
UseProperAssertMethodsAnalyzer.cs— InRecognizeCountCheck, returnCountCheckStatus.UnknownwhenexpectedArgument.Type?.SpecialType != SpecialType.System_Int32in theHasCountbranch. This covers all non-inttypes includingint?,long,uint,decimal, etc. Applies to both the BCL.Count/.Lengthproperty pattern and the LINQ.Count()method pattern. TheIsEmptybranch (expected == 0) is unaffected since the expected argument is removed entirely by the fix.UseProperAssertMethodsAnalyzerTests.cs— AddWhenAssertAreEqualWithCollectionCountNonZeroNonIntExpectedverifying no diagnostic is raised for both patterns when the expected value isint?orlong.Example
💬 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.