Skip to content

Fix MA0183 false positive when formatting argument is wrapped in parentheses#1044

Merged
meziantou merged 3 commits intomainfrom
copilot/fix-false-positive-ma0183
Feb 25, 2026
Merged

Fix MA0183 false positive when formatting argument is wrapped in parentheses#1044
meziantou merged 3 commits intomainfrom
copilot/fix-false-positive-ma0183

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Feb 25, 2026

MA0183 incorrectly fires when a formatting argument is wrapped in extra parentheses, even when the format string contains a valid placeholder.

// False positive — format string has {0} but diagnostic is reported
_ = string.Format(CultureInfo.InvariantCulture, SR.PlaceHolder, (obj is null ? string.Empty : "X"));

// Correct — no diagnostic
_ = string.Format(CultureInfo.InvariantCulture, SR.PlaceHolder, obj is null ? string.Empty : "X");

Root cause

HasFormattingArguments skipped arguments where IArgumentOperation.IsImplicit == true. Roslyn sets IsImplicit = true on parenthesized expressions even when ArgumentKind = Explicit, causing the argument to be treated as if it wasn't provided — resulting in a spurious "no formatting arguments" conclusion and a false positive diagnostic.

Fix

  • Replace if (arg.IsImplicit) continue; with if (arg.ArgumentKind is ArgumentKind.DefaultValue) continue; in HasFormattingArguments
  • DefaultValue correctly identifies arguments that were genuinely omitted (optional parameters using their defaults), without incorrectly skipping parenthesized expressions
Original prompt

This section details on the original issue you should resolve

<issue_title>MA0183: False positive when using extra parentheses</issue_title>
<issue_description>### Version of the Meziantou.Analyzer NuGet package

3.0.15

Rule Identifier

MA0183

Target Framework

.NET 10.0

C# Language version

C# 14

Description

When the "placeholder" argument to a formatting method is enclosed in extra parentheses, then MA0183 is wrongfully reported.

Reproduction Steps

Minimal code:

using System.Globalization;

_ = string.Format(CultureInfo.InvariantCulture, SR.PlaceHolder, (obj is null ? string.Empty : "X")); // NOK, MA0183 is reported
_ = string.Format(CultureInfo.InvariantCulture, SR.PlaceHolder, obj is null ? string.Empty : "X"); // // OK, MA0183 is not reported

_ = string.Format(CultureInfo.InvariantCulture, SR.NoPlaceHolder, (obj is null ? string.Empty : "X")); // OK, MA0183 is not reported
_ = string.Format(CultureInfo.InvariantCulture, SR.NoPlaceHolder, obj is null ? string.Empty : "X"); // OK, MA0183 is not reported

internal static class SR
{
    public const string PlaceHolder = "Format string with placeholder: '{0}'.";
    public const string NoPlaceHolder = "Format string without placeholder.";
}

Other information

No response</issue_description>

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


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 2 commits February 25, 2026 14:13
…guments

Co-authored-by: meziantou <509220+meziantou@users.noreply.github.com>
Co-authored-by: meziantou <509220+meziantou@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix false positive for MA0183 with extra parentheses Fix MA0183 false positive when formatting argument is wrapped in parentheses Feb 25, 2026
Copilot AI requested a review from meziantou February 25, 2026 14:16
@meziantou meziantou marked this pull request as ready for review February 25, 2026 15:11
@meziantou meziantou merged commit 5aab7d4 into main Feb 25, 2026
12 checks passed
@meziantou meziantou deleted the copilot/fix-false-positive-ma0183 branch February 25, 2026 15:30
This was referenced Feb 25, 2026
This was referenced May 1, 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.

MA0183: False positive when using extra parentheses

2 participants