Skip to content

Enhance MSTEST0037 to suggest Assert.IsNotEmpty for collection count patterns #6341

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Aug 11, 2025

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:

Assert.IsTrue(myList.Count > 0);  // Suggested: Assert.IsGreaterThan(0, myList.Count)
Assert.IsTrue(myList.Count != 0); // Suggested: Assert.AreNotEqual(0, myList.Count)

After this change:

Assert.IsTrue(myList.Count > 0);  // Now suggests: Assert.IsNotEmpty(myList)
Assert.IsTrue(myList.Count != 0); // Now suggests: Assert.IsNotEmpty(myList)

Implementation Details

  • Added RecognizeCollectionEmptinessCheck method to detect collection count/length patterns with zero
  • Supports both Count and Length properties on BCL collection types (List, arrays, etc.)
  • Handles reverse order patterns like 0 != myList.Count
  • Only applies to Assert.IsTrue() calls to maintain semantic correctness
  • Uses existing CodeFixModeSimple for seamless integration with the codefix
  • Prioritizes collection-specific suggestions before generic comparison logic

Comprehensive Coverage

The analyzer now handles these patterns:

  • myCollection.Count > 0Assert.IsNotEmpty(myCollection)
  • myCollection.Count != 0Assert.IsNotEmpty(myCollection)
  • myArray.Length > 0Assert.IsNotEmpty(myArray)
  • myArray.Length != 0Assert.IsNotEmpty(myArray)
  • 0 != myCollection.CountAssert.IsNotEmpty(myCollection) (reverse order)

While preserving existing behavior for:

  • myCollection.Count > 2Assert.IsGreaterThan(2, myCollection.Count)
  • myCollection.Count != 5Assert.AreNotEqual(5, myCollection.Count)
  • Assert.AreEqual(0, myCollection.Count)Assert.IsEmpty(myCollection)

Testing

Added comprehensive test coverage including:

  • Generic collections (List<T>)
  • Arrays (T[])
  • Reverse order patterns
  • Edge cases to ensure no regression
  • Non-BCL collections (correctly fall back to generic suggestions)

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
    • Triggering command: dotnet build src/Analyzers/MSTest.Analyzers/MSTest.Analyzers.csproj --verbosity quiet (dns block)
    • Triggering command: 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
    • Triggering command: dotnet build src/Analyzers/MSTest.Analyzers/MSTest.Analyzers.csproj --verbosity quiet (dns block)
    • Triggering command: 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
    • Triggering command: dotnet build src/Analyzers/MSTest.Analyzers/MSTest.Analyzers.csproj --verbosity quiet (dns block)
    • Triggering command: 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
    • Triggering command: dotnet build src/Analyzers/MSTest.Analyzers/MSTest.Analyzers.csproj --verbosity quiet (dns block)
    • Triggering command: 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
    • Triggering command: dotnet build src/Analyzers/MSTest.Analyzers/MSTest.Analyzers.csproj --verbosity quiet (dns block)
    • Triggering command: 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
    • Triggering command: dotnet build src/Analyzers/MSTest.Analyzers/MSTest.Analyzers.csproj --verbosity quiet (dns block)
    • Triggering command: 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
    • Triggering command: dotnet build src/Analyzers/MSTest.Analyzers/MSTest.Analyzers.csproj --verbosity quiet (dns block)
    • Triggering command: 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
    • Triggering command: dotnet build src/Analyzers/MSTest.Analyzers/MSTest.Analyzers.csproj --verbosity quiet (dns block)
    • Triggering command: 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.

@Copilot Copilot AI changed the title [WIP] Update MSTEST0037 to propose Assert.IsNotEmpty for more cases Enhance MSTEST0037 to suggest Assert.IsNotEmpty for collection count patterns Aug 11, 2025
@Copilot Copilot AI requested a review from Evangelink August 11, 2025 08:46
Copilot finished work on behalf of Evangelink August 11, 2025 08:46
@Evangelink Evangelink marked this pull request as ready for review August 13, 2025 09:10
@Evangelink Evangelink enabled auto-merge (squash) August 13, 2025 09:10
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.

Update MSTEST0037 to propose Assert.IsNotEmpty for more cases
2 participants