Fix MA0002 false positive on interpolated string handlers and invalid code fix on named arguments - #1289
Merged
Merged
Conversation
) MA0002 reported a diagnostic on compiler-generated interpolated string handler creations (e.g. MSTest's AssertAreEqualInterpolatedStringHandler<T> when calling Assert.AreEqual(a, b, $"msg")). There is no argument list in the source code where a comparer could be added, so the diagnostic is not actionable. Implicit object creations are now skipped. The code fix also generated code that doesn't compile when the invocation already used named arguments: AreEqual(actual: b, expected: a) became AreEqual(actual: b, expected: a, StringComparer.Ordinal) which reports CS8323. The comparer is now added as a named argument in that case, and the fix is not registered when the parameter name is unknown. Add regression tests using the real MSTest.TestFramework package for the scenario reported in #1249.
This was referenced Aug 17, 2026
Bump Meziantou.Analyzer from 3.0.103 to 3.0.163
Analogy-LogViewer/Analogy.LogViewer.OpenTelemetry#90
Open
Open
Open
Bump Meziantou.Analyzer from 3.0.139 to 3.0.163
Analogy-LogViewer/Analogy.LogViewer.NLog.Targets#549
Open
This was referenced Aug 18, 2026
Open
Bump Meziantou.Analyzer from 3.0.139 to 3.0.163
Analogy-LogViewer/Analogy.AspNetCore.LogProvider#535
Open
Open
Open
Open
Open
Open
Open
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.
Follow-up investigation of #1249.
The originally reported case is already fixed
I could not reproduce the case from the issue comment on
3.0.159. Built against the realMSTest.TestFrameworkpackage,Assert.AreEqual("id", fields[0], "First field should be the ID")correctly produces:Verified across MSTest 3.0.4 / 3.6.4 / 3.9.3 / 4.0.3 / 4.3.3, Roslyn 4.8 / 4.14 / 5.0, C# 9 / 10 / 13, and both
#nullable enableand#nullable disable. #1252 shipped in 3.0.136, so 3.0.159 contains it — the report is most likely a stale analyzer in the IDE's analyzer host.This PR adds regression tests locking that behavior in, plus fixes for two genuine bugs found while investigating.
MA0002 false positive on interpolated string handlers
Assert.AreEqual(a, b, $"msg {x}")binds to MSTest'sAssertAreEqualInterpolatedStringHandler<T>overload. The analyzer reported a second diagnostic on the compiler-generated handler construction (located on the$"msg {x}"expression itself), because that handler's constructor also has an overload taking anIEqualityComparer<T>.That diagnostic is not actionable — there is no argument list in the source code where a comparer could be added — and the code fix registered for it mangles the call.
AnalyzeConstructornow skips implicit object creations.Code fix generated code that doesn't compile with named arguments
Given:
the fix produced:
which fails with
CS8323: Named argument 'actual' is used out-of-position but is followed by an unnamed argument.The comparer is now emitted as a named argument whenever the argument list already contains named arguments, and the fix is no longer registered when the parameter name could not be determined (per the "validate before registering" guidance in
AGENTS.md). The three duplicated insert/append blocks in the fixer were collapsed into a single helper.Tests
Three tests added to
UseStringComparerAnalyzerTests; two of them reference the real MSTest 4.3.3 package:CodeFix_MSTestAreEqualWithMessage_Issue1249CodeFix_MSTestAreEqualWithInterpolatedMessage_Issue1249CodeFix_UseNamedArgumentWhenArgumentsAreNamedThe test harness compiles the fixed code, so the
CS8323case fails loudly without the fixer change.Verification
dotnet run --project src/DocumentationGeneratorexits 0 with no markdown changes.