Skip to content

Fix MSTEST0037 to report on Assert.IsFalse with string and collection methods#6178

Merged
Evangelink merged 4 commits intomainfrom
copilot/fix-6177
Jul 28, 2025
Merged

Fix MSTEST0037 to report on Assert.IsFalse with string and collection methods#6178
Evangelink merged 4 commits intomainfrom
copilot/fix-6177

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Jul 25, 2025

The MSTEST0037 analyzer was not reporting diagnostics for Assert.IsFalse with string and collection methods, even though MSTest provides appropriate negative assertion methods for these cases.

Problem

Previously, these patterns would not trigger MSTEST0037:

Assert.IsFalse(myString.Contains("test"));     // No diagnostic
Assert.IsFalse(myString.StartsWith("Hello"));  // No diagnostic  
Assert.IsFalse(myString.EndsWith("World"));    // No diagnostic
Assert.IsFalse(myList.Contains(item));         // No diagnostic

Solution

The analyzer now detects these patterns and suggests the appropriate negative assertion methods:

// Before: No diagnostics
Assert.IsFalse(myString.Contains("test"));
Assert.IsFalse(myString.StartsWith("Hello"));
Assert.IsFalse(myString.EndsWith("World"));
Assert.IsFalse(myList.Contains(item));

// After: MSTEST0037 diagnostics with code fixes
Assert.DoesNotContain("test", myString);
Assert.DoesNotStartWith("Hello", myString);  
Assert.DoesNotEndWith("World", myString);
Assert.DoesNotContain(item, myList);

Changes Made

  1. Analyzer Enhancement: Modified UseProperAssertMethodsAnalyzer.AnalyzeIsTrueOrIsFalseInvocation to handle IsFalse cases with string and collection methods
  2. Documentation Update: Updated XML documentation to include the new supported patterns
  3. Test Coverage: Added comprehensive tests for all new diagnostic patterns with expected code fixes
  4. Backward Compatibility: All existing functionality remains unchanged

The implementation leverages the existing CodeFixModeAddArgument infrastructure, so code fixes work seamlessly with the new patterns.

Fixes #6177.

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:

  • jrqvsblobprodcus343.vsblob.vsassets.io
    • Triggering command: dotnet restore src/Analyzers/MSTest.Analyzers/MSTest.Analyzers.csproj (dns block)
    • Triggering command: /home/REDACTED/work/_temp/ghcca-node/node/bin/node --enable-source-maps /home/REDACTED/work/_temp/copilot-developer-action-main/dist/index.js (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 AI changed the title [WIP] MSTEST0037 does not report on Assert.IsFalse Fix MSTEST0037 to report on Assert.IsFalse with string and collection methods Jul 25, 2025
Copilot AI requested a review from Evangelink July 25, 2025 17:22
Copilot AI requested a review from Evangelink July 25, 2025 20:36
@Evangelink Evangelink marked this pull request as ready for review July 25, 2025 20:54
@Evangelink Evangelink enabled auto-merge (squash) July 25, 2025 20:54
@Evangelink Evangelink merged commit a682b53 into main Jul 28, 2025
9 checks passed
@Evangelink Evangelink deleted the copilot/fix-6177 branch July 28, 2025 08:38
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 report on Assert.IsFalse

4 participants