Skip to content

MA0074: Fix StringComparison argument inserted at wrong position when trailing parameters exist#1050

Merged
meziantou merged 3 commits intomainfrom
copilot/fix-assert-contains-mstest
Mar 4, 2026
Merged

MA0074: Fix StringComparison argument inserted at wrong position when trailing parameters exist#1050
meziantou merged 3 commits intomainfrom
copilot/fix-assert-contains-mstest

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 4, 2026

  • Understand the issue: MA0074 code fixer appends StringComparison at the end of argument list instead of at the correct position
  • Modify UseStringComparisonFixer.cs to find the correct index for the StringComparison argument based on the target overload's parameter list
    • Add OverloadFinder, OverloadOptions, OverloadParameterType to CodeFixers project
    • Replace duplicated overload-finding logic with OverloadFinder.FindOverloadWithAdditionalParameterOfType
  • Add test case Contains_WithTrailingMessageParameter_ShouldInsertStringComparisonBeforeMessage
  • All 12 tests pass
Original prompt

This section details on the original issue you should resolve

<issue_title>MA0074: fix doesn't work with MSTest Assert.Contains when a message is present</issue_title>
<issue_description>### Version of the Meziantou.Analyzer NuGet package

3.0.18

Rule Identifier

MA0074

Target Framework

net9.0

C# Language version

C# 13

Description

Trying to fix MA0074 with the provided fix on a MSTest Assert.Contains that has a message in the call, will result in non-compilable code.

Reproduction Steps

Minimal code:

const string TEST_ID = "C12345678";
string csv = okResult.Value.ToString();
Assert.Contains(TEST_ID, csv, "CSV should contain the provided ID"); // This will break when applying the fix for MA0074

Applyinng the fix will result with this line:

Assert.Contains(TEST_ID, csv, "CSV should contain the provided ID", StringComparison.OrdinalIgnoreCase);

when it should be

Assert.Contains(TEST_ID, csv, StringComparison.OrdinalIgnoreCase, "CSV should contain the provided ID");

Other information

No response</issue_description>

<agent_instructions>The fix would be to find the index of the StringComparison parameter, so the code fixer can add the argument at the right index</agent_instructions>

Comments on the Issue (you are @copilot in this section)


🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. Learn more about Advanced Security.

…ment position

Co-authored-by: meziantou <509220+meziantou@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix MA0074 for MSTest Assert.Contains with message MA0074: Fix StringComparison argument inserted at wrong position when trailing parameters exist Mar 4, 2026
return -1;

var targetMethod = operation.TargetMethod;
foreach (var member in targetMethod.ContainingType.GetMembers(targetMethod.Name))
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

@copilot Can you reuse the logic from the analyzer to find an overload instead of duplicating it. If the analyzer logic changed, it should be reflected in the fixer.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done in d338601. Added OverloadFinder, OverloadOptions, and OverloadParameterType to the CodeFixers project, then replaced the duplicated overload-finding logic with overloadFinder.FindOverloadWithAdditionalParameterOfType(operation, options: default, [stringComparison]). The FindStringComparisonParameterIndex method now just uses the existing OverloadFinder and extracts the position of the StringComparison parameter from the returned overload.

…arameter index

Co-authored-by: meziantou <509220+meziantou@users.noreply.github.com>
@meziantou meziantou marked this pull request as ready for review March 4, 2026 05:50
@meziantou meziantou merged commit cbdeda3 into main Mar 4, 2026
12 checks passed
@meziantou meziantou deleted the copilot/fix-assert-contains-mstest branch March 4, 2026 05:56
This was referenced Apr 6, 2026
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.

MA0074: fix doesn't work with MSTest Assert.Contains when a message is present

2 participants