Fix MA0002 code fix inserting StringComparer at wrong argument position - #1252
Merged
Merged
Conversation
The UseStringComparerFixer was always appending the StringComparer argument at the end of the argument list, breaking the build when the target overload has the comparer parameter before other parameters (e.g., a message or a CancellationToken). Fixes: - Issue #1249: Assert.AreEqual(expected, actual, message) would become AreEqual(expected, actual, message, StringComparer.Ordinal) instead of AreEqual(expected, actual, StringComparer.Ordinal, message). - Issue #1250: Extension method calls where the comparer comes before other parameters (e.g., CancellationToken). The fix follows the UseIFormatProviderFixer pattern: 1. Find the target overload via OverloadFinder. 2. Determine the correct insertion index via TryGetComparerParameterInfo, which strips the implicit 'this' parameter from extension methods so the index aligns with the actual argument list. 3. Insert positionally when index <= current arg count, or use a named argument when the comparer falls beyond the current args (optional params). Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
This was referenced Jul 29, 2026
Closed
Closed
Closed
Closed
Closed
Closed
Closed
Closed
Closed
Closed
Closed
Bump Meziantou.Analyzer from 3.0.103 to 3.0.136
Analogy-LogViewer/Analogy.LogViewer.OpenTelemetry#77
Closed
Closed
Closed
This was referenced Aug 6, 2026
Open
Open
Open
Open
Bump Meziantou.Analyzer from 3.0.103 to 3.0.140
Analogy-LogViewer/Analogy.LogViewer.OpenTelemetry#81
Open
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.
Fixes #1249 and #1250.
Problem
The
UseStringComparerFixer(MA0002) was always appending theStringComparerargument at the end of the argument list. This broke the build when the target overload has the comparer parameter before other parameters:Assert.AreEqual(expected, actual, message)→ incorrectly becameAreEqual(expected, actual, message, StringComparer.Ordinal)instead ofAreEqual(expected, actual, StringComparer.Ordinal, message)CancellationToken→ same issueFix
The fix follows the existing
UseIFormatProviderFixerpattern:OverloadFinder.FindOverloadWithAdditionalParameterOfTypeTryGetComparerParameterInfohelper that:thisparameter from extension methods (so the index aligns with the actual argument list, not the full parameter list)index <= currentArgCount, or use a named argument when the comparer falls beyond the current args (handles optional-params-before-comparer edge case)The key insight for #1250 (extension methods):
IInvocationOperation.TargetMethodreturns the non-reduced extension method (includingthisin its parameters), but the argument list doesn't include the receiver. The newGetComparableParametershelper always stripsthisfrom extension methods regardless of form (reduced or not), ensuring the insertion index maps correctly to the argument list position.