Skip to content

Add MA0185: Simplify string.Create with culture-invariant parameters#986

Merged
meziantou merged 6 commits intomainfrom
copilot/report-diagnostic-for-string-create
Feb 1, 2026
Merged

Add MA0185: Simplify string.Create with culture-invariant parameters#986
meziantou merged 6 commits intomainfrom
copilot/report-diagnostic-for-string-create

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Feb 1, 2026

  • Add new rule identifier MA0185 for "Simplify string.Create when all parameters are culture invariant"
  • Create analyzer to detect string.Create(CultureInfo.InvariantCulture, $"...") with only culture-invariant parameters
  • Create code fixer to replace with simple interpolated string $"..."
  • Add comprehensive tests for the analyzer
  • Update documentation using DocumentationGenerator
  • Change rule ID from MA0184 to MA0185 per review feedback
  • Fix compilation error by wrapping IInterpolatedStringHandlerCreationOperation usage with CSHARP10_OR_GREATER directive
Original prompt

This section details on the original issue you should resolve

<issue_title>Report diagnostic when using string.Create(CultureInfo, ref DefaultInterpolatedStringHandler) with only culture invariant parameters</issue_title>
<issue_description>Using string.Create(CultureInfo, ref DefaultInterpolatedStringHandler) with an interpolated string of which all parameters are guaranteed to be invariant can be simplified.

var x = string.Create(CultureInfo.InvariantCulture, $"Current time is {DateTime.Now:O}."); // NOK
var y = $"Current time is {DateTime.Now:O}."; // OK

</issue_description>

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


✨ 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 and others added 2 commits February 1, 2026 20:26
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] Report diagnostic for string.Create with invariant parameters Add MA0184: Simplify string.Create with culture-invariant parameters Feb 1, 2026
Copilot AI requested a review from meziantou February 1, 2026 20:33
Comment thread docs/Rules/MA0184.md Outdated
@@ -0,0 +1,28 @@
# MA0184 - Simplify string.Create when all parameters are culture invariant
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 Change the ID to MA0185

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.

Changed from MA0184 to MA0185 in commit a606cd8.

Co-authored-by: meziantou <509220+meziantou@users.noreply.github.com>
Copilot AI changed the title Add MA0184: Simplify string.Create with culture-invariant parameters Add MA0185: Simplify string.Create with culture-invariant parameters Feb 1, 2026
Copilot AI requested a review from meziantou February 1, 2026 21:19
@meziantou
Copy link
Copy Markdown
Owner

@copilot Can you regenerate the documentation?

Copy link
Copy Markdown
Contributor Author

Copilot AI commented Feb 1, 2026

@copilot Can you regenerate the documentation?

The documentation is already up to date. I ran the DocumentationGenerator and it confirmed no changes were needed. Both MA0184 (from the main branch merge) and MA0185 (this PR's rule) are properly documented in README.md, docs/README.md, and their respective rule documentation files.

@meziantou
Copy link
Copy Markdown
Owner

@copilot

Error: /home/runner/work/Meziantou.Analyzer/Meziantou.Analyzer/src/Meziantou.Analyzer/Rules/SimplifyStringCreateWhenAllParametersAreCultureInvariantAnalyzer.cs(74,43): error CS0246: The type or namespace name 'IInterpolatedStringHandlerCreationOperation' could not be found (are you missing a using directive or an assembly reference?) [/home/runner/work/Meziantou.Analyzer/Meziantou.Analyzer/src/Meziantou.Analyzer/Meziantou.Analyzer.csproj::TargetFramework=net10.0]
Error: /home/runner/work/Meziantou.Analyzer/Meziantou.Analyzer/src/Meziantou.Analyzer/Rules/SimplifyStringCreateWhenAllParametersAreCultureInvariantAnalyzer.cs(74,43): error CS0246: The type or namespace name 'IInterpolatedStringHandlerCreationOperation' could not be found (are you missing a using directive or an assembly reference?) [/home/runner/work/Meziantou.Analyzer/Meziantou.Analyzer/src/Meziantou.Analyzer/Meziantou.Analyzer.csproj::TargetFramework=netstandard2.0]

Co-authored-by: meziantou <509220+meziantou@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Feb 1, 2026

@copilot

Error: /home/runner/work/Meziantou.Analyzer/Meziantou.Analyzer/src/Meziantou.Analyzer/Rules/SimplifyStringCreateWhenAllParametersAreCultureInvariantAnalyzer.cs(74,43): error CS0246: The t...

Fixed in commit d582111. Added #if CSHARP10_OR_GREATER preprocessor directive around the IInterpolatedStringHandlerCreationOperation usage to match the pattern used in CultureSensitiveFormattingContext.

@meziantou meziantou marked this pull request as ready for review February 1, 2026 21:31
@meziantou meziantou enabled auto-merge (squash) February 1, 2026 21:32
@meziantou meziantou merged commit 1326bdb into main Feb 1, 2026
13 checks passed
@meziantou meziantou deleted the copilot/report-diagnostic-for-string-create branch February 1, 2026 21:33
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.

Report diagnostic when using string.Create(CultureInfo, ref DefaultInterpolatedStringHandler) with only culture invariant parameters

2 participants