Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 4, 2025

  • Explore repository structure and understand MA0011 rule
  • Review UseIFormatProviderAnalyzer.cs and CultureSensitiveFormattingContext.cs
  • Understand the issue: MA0011 should not be reported when interpolated string is culture-invariant
  • Modify CultureSensitiveFormattingContext to detect culture-invariant interpolated strings
  • Create comprehensive unit tests based on issue examples
  • Build and verify changes compile
  • Run documentation generator (no changes needed)
  • Address code review feedback
  • Run CodeQL security check (no issues found)
  • Fix file encoding issue (UTF-8 with BOM)
  • Refactor: Move logic from UseIFormatProviderAnalyzer to CultureSensitiveFormattingContext
  • Remove redundant DefaultInterpolatedStringHandler check

Summary

Fixed MA0011 to skip reporting diagnostics when interpolated string arguments are culture-invariant.

Changes:

  1. CultureSensitiveFormattingContext.cs: Added detection for FormattableString and InterpolatedStringHandler arguments to check if they contain only culture-invariant content. Removed redundant check for DefaultInterpolatedStringHandler since it's covered by the InterpolatedStringHandlerAttribute check.
  2. UseIFormatProviderAnalyzer.cs: Reverted to original state (only UTF-8 BOM encoding added)
  3. UseIFormatProviderAnalyzerTests.cs: Comprehensive test cases added by @meziantou

The logic now properly resides in CultureSensitiveFormattingContext where it can be reused by other analyzers.

Original prompt

This section details on the original issue you should resolve

<issue_title>MA0011 should not be reported when format is culture-invariant</issue_title>
<issue_description>### Version of the Meziantou.Analyzer NuGet package

2.0.243

Rule Identifier

MA0011

Target Framework

.NET 9.0

C# Language version

4.14.0-3.25412.6 (c167adef)

Description

MA0011 is currently reported when there's a method for which there's an overload taking a interpolated string and an overload taking both and IFormatProvider and an interpolated string, and that method is invoked with a fully culture-invariant interpolated string.

Reproduction Steps

Minimal code:

using System;
using System.Runtime.CompilerServices;

using Meziantou.Analyzer.Annotations;

internal sealed class Program
{
    private static void Main(string[] args)
    {
        Print($"{DateTime.Now:D}"); // MA0011 is reported. This is fine as the "D" format is culture-sensitive
        Print($"{new Bar():D}"); // MA0011 is reported. This is fine as the "D" format is culture-sensitive
        Print($"{new Bar():o} | {new Bar():D}"); // MA0011 is reported. This is fine as the "D" format is culture-sensitive

        Print($"{DateTime.Now:o}"); // MA0011 should not be reported as the "o" format is culture invariant
        Print($"{new Bar():o}"); // MA0011 should not be reported as the "o" format is culture invariant
        Print($"XXX"); // MA0011 should not be reported because ... :p
    }

    public static void Print(ref DefaultInterpolatedStringHandler interpolatedStringHandler)
    {
    }

    public static void Print(IFormatProvider provider, ref DefaultInterpolatedStringHandler interpolatedStringHandler)
    {
    }
}

[CultureInsensitiveType(format: "o")]
public sealed class Bar : IFormattable
{
    public string ToString(string? format, IFormatProvider? formatProvider)
    {
        return string.Empty;
    }
}

Other information

No response</issue_description>

<agent_instructions>Look at the last comment to better understand what is expected to update the MA0011 rule</agent_instructions>

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

@meziantou That's a good suggestion. I'll need to figure out the right way to handle it with all the cases that may exist with string interpolations. I'll let copilot generate some unit tests. That may help to shape the solution. @meziantou The rule may be like, - Analyze method invocation - If it has an argument of type `IFormatProvider` => do not report - If there is no overload with a `IFormatProvider` parameter => do not report - If a parameter is of type `FormattableString` or a type decorated with `[InterpolatedStringHandler]`, and the argument is a not culture sensitive interpolated string => do not report - Otherwise, report

Examples:

var value = DateTime.UtcNow;

// There is no overload available => ok
Sample1($"{value}"); // ok
Sample1($"{value:o}"); // ok

void Sample1(ref DefaultInterpolatedStringHandler handler) => throw null;
// Overload with DefaultInterpolatedStringHandler, a type decorated with InterpolatedStringHandler
Sample2($"{value}"); // non-compliant
Sample2(CultureInfo.InvariantCulture, $"{value}"); // ok
Sample2($"{value:o}"); // ok

void Sample2(ref DefaultInterpolatedStringHandler handler) => throw null;
void Sample2(IFormatProvider format, ref DefaultInterpolatedStringHandler handler) => throw null;
// Overload with FormattableString
Sample3($"{value}"); // non-compliant
Sample3(CultureInfo.InvariantCulture, $"{value}"); // ok
Sample3($"{value:o}"); // ok

void Sample3(FormattableString value) => throw null;
void Sample3(IFormatProvider format, FormattableString value) => throw null;
````</body></comment_new>
</comments>

✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI changed the title [WIP] Fix MA0011 reporting for culture-invariant format MA0011: Skip diagnostic for culture-invariant interpolated strings Nov 4, 2025
Copilot AI requested a review from meziantou November 4, 2025 02:52
@meziantou
Copy link
Owner

@copilot Look at the ci output and fix the error

Copy link
Contributor Author

Copilot AI commented Nov 4, 2025

@copilot Look at the ci output and fix the error

Fixed the file encoding issue in commit 9df0dfa. Both UseIFormatProviderAnalyzer.cs and UseIFormatProviderAnalyzerTests.cs now have the correct UTF-8 with BOM encoding required by the formatter.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • www.nuget.org
    • Triggering command: /usr/share/dotnet/dotnet exec --runtimeconfig /home/REDACTED/work/Meziantou.Analyzer/Meziantou.Analyzer/tests/Meziantou.Analyzer.Test/bin/Release/net10.0/Meziantou.Analyzer.Test.runtimeconfig.json --depsfile /home/REDACTED/work/Meziantou.Analyzer/Meziantou.Analyzer/tests/Meziantou.Analyzer.Test/bin/Release/net10.0/Meziantou.Analyzer.Test.deps.json /home/REDACTED/work/Meziantou.Analyzer/Meziantou.Analyzer/tests/Meziantou.Analyzer.Test/bin/Release/net10.0/testhost.dll --port 46527 --endpoint 127.0.0.1:046527 --role client --parentprocessid 4311 --datacollectionport 34917 --telemetryoptedin false (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

@meziantou meziantou force-pushed the copilot/fix-ma0011-reporting-issue branch from 9df0dfa to 8a8cfc7 Compare November 4, 2025 03:53
@meziantou
Copy link
Owner

@copilot I've updated the tests.
You have update the code in UseIFormatProviderAnalyzer, but you should have updated the CultureSensitiveFormattingContext class to detect the arguments contains an interpolated string. Please revert changes in UseIFormatProviderAnalyzer and update the logic in CultureSensitiveFormattingContext

Copy link
Contributor Author

Copilot AI commented Nov 4, 2025

@copilot I've updated the tests.
You have update the code in UseIFormatProviderAnalyzer, but you should have updated the CultureSensitiveFormattingContext class to detect the arguments contains an int...

Refactored in commit 409df3f. Reverted changes in UseIFormatProviderAnalyzer.cs and moved the interpolated string detection logic to CultureSensitiveFormattingContext.cs where it can be reused by other analyzers. The logic now checks if all interpolated string arguments (FormattableString or types with InterpolatedStringHandlerAttribute) are culture-invariant before reporting them as culture-sensitive.

if (typeSymbol.IsEqualTo(FormattableStringSymbol))
return true;

if (typeSymbol.IsEqualTo(DefaultInterpolatedStringHandlerSymbol))
Copy link
Owner

Choose a reason for hiding this comment

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

@copilot Checking this type is useless. Checking the InterpolatedStringHandlerAttributeSymbol attribute here-after is enough.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed in commit b685ca8. The DefaultInterpolatedStringHandler check and symbol field have been removed since DefaultInterpolatedStringHandler has the InterpolatedStringHandlerAttribute, making the explicit check redundant.

@meziantou meziantou force-pushed the copilot/fix-ma0011-reporting-issue branch from 8176922 to c0ce4ba Compare November 4, 2025 05:20
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.

MA0011 should not be reported when format is culture-invariant

2 participants