Enhance MSTEST0037 to suggest Assert.IsNotEmpty for collection count patterns #6341
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.
The MSTEST0037 analyzer now detects collection emptiness patterns and suggests
Assert.IsNotEmpty
instead of generic comparison/equality assertions for better readability and intent clarity.Changes Made
Before this change:
After this change:
Implementation Details
RecognizeCollectionEmptinessCheck
method to detect collection count/length patterns with zeroCount
andLength
properties on BCL collection types (List, arrays, etc.)0 != myList.Count
Assert.IsTrue()
calls to maintain semantic correctnessCodeFixModeSimple
for seamless integration with the codefixComprehensive Coverage
The analyzer now handles these patterns:
myCollection.Count > 0
→Assert.IsNotEmpty(myCollection)
myCollection.Count != 0
→Assert.IsNotEmpty(myCollection)
myArray.Length > 0
→Assert.IsNotEmpty(myArray)
myArray.Length != 0
→Assert.IsNotEmpty(myArray)
0 != myCollection.Count
→Assert.IsNotEmpty(myCollection)
(reverse order)While preserving existing behavior for:
myCollection.Count > 2
→Assert.IsGreaterThan(2, myCollection.Count)
myCollection.Count != 5
→Assert.AreNotEqual(5, myCollection.Count)
Assert.AreEqual(0, myCollection.Count)
→Assert.IsEmpty(myCollection)
Testing
Added comprehensive test coverage including:
List<T>
)T[]
)Fixes #6312.
Warning
Firewall rules blocked me from connecting to one or more addresses
I tried to connect to the following addresses, but was blocked by firewall rules:
4m6vsblobprodcus384.vsblob.vsassets.io
dotnet build src/Analyzers/MSTest.Analyzers/MSTest.Analyzers.csproj --verbosity quiet
(dns block)dotnet test test/UnitTests/MSTest.Analyzers.UnitTests/MSTest.Analyzers.UnitTests.csproj --filter ClassName=UseProperAssertMethodsAnalyzerTests --verbosity minimal --no-restore --no-build
(dns block)4zjvsblobprodcus390.vsblob.vsassets.io
dotnet build src/Analyzers/MSTest.Analyzers/MSTest.Analyzers.csproj --verbosity quiet
(dns block)dotnet test test/UnitTests/MSTest.Analyzers.UnitTests/MSTest.Analyzers.UnitTests.csproj --filter ClassName=UseProperAssertMethodsAnalyzerTests --verbosity minimal --no-restore --no-build
(dns block)6s7vsblobprodcus313.vsblob.vsassets.io
dotnet build src/Analyzers/MSTest.Analyzers/MSTest.Analyzers.csproj --verbosity quiet
(dns block)dotnet test test/UnitTests/MSTest.Analyzers.UnitTests/MSTest.Analyzers.UnitTests.csproj --filter ClassName=UseProperAssertMethodsAnalyzerTests --verbosity minimal --no-restore --no-build
(dns block)ckzvsblobprodcus347.vsblob.vsassets.io
dotnet build src/Analyzers/MSTest.Analyzers/MSTest.Analyzers.csproj --verbosity quiet
(dns block)dotnet test test/UnitTests/MSTest.Analyzers.UnitTests/MSTest.Analyzers.UnitTests.csproj --filter ClassName=UseProperAssertMethodsAnalyzerTests --verbosity minimal --no-restore --no-build
(dns block)l49vsblobprodcus358.vsblob.vsassets.io
dotnet build src/Analyzers/MSTest.Analyzers/MSTest.Analyzers.csproj --verbosity quiet
(dns block)dotnet test test/UnitTests/MSTest.Analyzers.UnitTests/MSTest.Analyzers.UnitTests.csproj --filter ClassName=UseProperAssertMethodsAnalyzerTests --verbosity minimal --no-restore --no-build
(dns block)m16vsblobprodcus374.vsblob.vsassets.io
dotnet build src/Analyzers/MSTest.Analyzers/MSTest.Analyzers.csproj --verbosity quiet
(dns block)dotnet test test/UnitTests/MSTest.Analyzers.UnitTests/MSTest.Analyzers.UnitTests.csproj --filter ClassName=UseProperAssertMethodsAnalyzerTests --verbosity minimal --no-restore --no-build
(dns block)pe3vsblobprodcus354.vsblob.vsassets.io
dotnet build src/Analyzers/MSTest.Analyzers/MSTest.Analyzers.csproj --verbosity quiet
(dns block)dotnet test test/UnitTests/MSTest.Analyzers.UnitTests/MSTest.Analyzers.UnitTests.csproj --filter ClassName=UseProperAssertMethodsAnalyzerTests --verbosity minimal --no-restore --no-build
(dns block)ytvvsblobprodcus310.vsblob.vsassets.io
dotnet build src/Analyzers/MSTest.Analyzers/MSTest.Analyzers.csproj --verbosity quiet
(dns block)dotnet test test/UnitTests/MSTest.Analyzers.UnitTests/MSTest.Analyzers.UnitTests.csproj --filter ClassName=UseProperAssertMethodsAnalyzerTests --verbosity minimal --no-restore --no-build
(dns block)If you need me to access, download, or install something from one of these locations, you can either:
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.