Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion docs/Rules/MA0185.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,15 @@
Sources: [SimplifyStringCreateWhenAllParametersAreCultureInvariantAnalyzer.cs](https://github.com/meziantou/Meziantou.Analyzer/blob/main/src/Meziantou.Analyzer/Rules/SimplifyStringCreateWhenAllParametersAreCultureInvariantAnalyzer.cs), [SimplifyStringCreateWhenAllParametersAreCultureInvariantFixer.cs](https://github.com/meziantou/Meziantou.Analyzer/blob/main/src/Meziantou.Analyzer.CodeFixers/Rules/SimplifyStringCreateWhenAllParametersAreCultureInvariantFixer.cs)
<!-- sources -->

When using `string.Create(CultureInfo.InvariantCulture, ...)` with an interpolated string where all parameters are culture-invariant, you can simplify the code by using a simple interpolated string instead.
When using `string.Create(CultureInfo.InvariantCulture, ...)` with an interpolated string where all parameters are culture-invariant (or no parameters at all), you can simplify the code by using a simple interpolated string instead.

````c#
// ❌ Unnecessary use of string.Create with literal-only string
var a = string.Create(CultureInfo.InvariantCulture, $"string without parameters.");

// ✅ Simplified version
var b = $"string without parameters.";

// ❌ Unnecessary use of string.Create with culture-invariant parameters
var x = string.Create(CultureInfo.InvariantCulture, $"Current time is {DateTime.Now:O}.");

Expand All @@ -14,6 +20,7 @@ var y = $"Current time is {DateTime.Now:O}.";
````

The analyzer detects when all interpolated values are culture-invariant, such as:
- **No parameters at all** (literal-only interpolated strings)
- Strings
- GUIDs
- DateTime with invariant formats (O, o, R, r, s, u)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,5 +290,77 @@ await CreateProjectBuilder()
.WithSourceCode(SourceCode)
.ValidateAsync();
}

[Fact]
public async Task StringCreateWithInvariantCulture_EmptyString_ShouldReport()
{
const string SourceCode = """
using System;
using System.Globalization;

class TypeName
{
public void Test()
{
var x = [|string.Create(CultureInfo.InvariantCulture, $"")|];
}
}
""";

const string Fix = """
using System;
using System.Globalization;

class TypeName
{
public void Test()
{
var x = $"";
}
}
""";
await CreateProjectBuilder()
.WithLanguageVersion(Microsoft.CodeAnalysis.CSharp.LanguageVersion.CSharp10)
.WithTargetFramework(TargetFramework.Net6_0)
.WithSourceCode(SourceCode)
.ShouldFixCodeWith(Fix)
.ValidateAsync();
}

[Fact]
public async Task StringCreateWithInvariantCulture_MultipleWords_ShouldReport()
{
const string SourceCode = """
using System;
using System.Globalization;

class TypeName
{
public void Test()
{
var x = [|string.Create(CultureInfo.InvariantCulture, $"This is a test message without any interpolations")|];
}
}
""";

const string Fix = """
using System;
using System.Globalization;

class TypeName
{
public void Test()
{
var x = $"This is a test message without any interpolations";
}
}
""";
await CreateProjectBuilder()
.WithLanguageVersion(Microsoft.CodeAnalysis.CSharp.LanguageVersion.CSharp10)
.WithTargetFramework(TargetFramework.Net6_0)
.WithSourceCode(SourceCode)
.ShouldFixCodeWith(Fix)
.ValidateAsync();
}
#endif
}