diff --git a/docs/Rules/MA0185.md b/docs/Rules/MA0185.md index b25ca178..4fb10f1c 100644 --- a/docs/Rules/MA0185.md +++ b/docs/Rules/MA0185.md @@ -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) -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}."); @@ -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) diff --git a/tests/Meziantou.Analyzer.Test/Rules/SimplifyStringCreateWhenAllParametersAreCultureInvariantAnalyzerTests.cs b/tests/Meziantou.Analyzer.Test/Rules/SimplifyStringCreateWhenAllParametersAreCultureInvariantAnalyzerTests.cs index 94ff1265..07720b52 100644 --- a/tests/Meziantou.Analyzer.Test/Rules/SimplifyStringCreateWhenAllParametersAreCultureInvariantAnalyzerTests.cs +++ b/tests/Meziantou.Analyzer.Test/Rules/SimplifyStringCreateWhenAllParametersAreCultureInvariantAnalyzerTests.cs @@ -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 }