diff --git a/src/Meziantou.Analyzer/Rules/StringFormatShouldBeConstantAnalyzer.cs b/src/Meziantou.Analyzer/Rules/StringFormatShouldBeConstantAnalyzer.cs index 8ed953d9..0b37e4e9 100644 --- a/src/Meziantou.Analyzer/Rules/StringFormatShouldBeConstantAnalyzer.cs +++ b/src/Meziantou.Analyzer/Rules/StringFormatShouldBeConstantAnalyzer.cs @@ -77,7 +77,20 @@ private static void Analyze(OperationAnalysisContext context) { var arg = operation.Arguments[i]; - // Skip implicit arguments (e.g., empty params array) + // Check if this is a params array argument + if (arg.ArgumentKind == ArgumentKind.ParamArray && arg.Value is IArrayCreationOperation arrayCreation) + { + // Check if the array has any elements + if (arrayCreation.Initializer is not null && arrayCreation.Initializer.ElementValues.Length > 0) + { + hasFormattingArguments = true; + break; + } + // Skip empty params arrays + continue; + } + + // Skip other implicit arguments if (arg.IsImplicit) continue; diff --git a/tests/Meziantou.Analyzer.Test/Rules/StringFormatShouldBeConstantAnalyzerTests.cs b/tests/Meziantou.Analyzer.Test/Rules/StringFormatShouldBeConstantAnalyzerTests.cs index a6fa30c4..ce23fa42 100644 --- a/tests/Meziantou.Analyzer.Test/Rules/StringFormatShouldBeConstantAnalyzerTests.cs +++ b/tests/Meziantou.Analyzer.Test/Rules/StringFormatShouldBeConstantAnalyzerTests.cs @@ -419,4 +419,44 @@ void Method() """) .ValidateAsync(); } + + [Fact] + public async Task StringFormat_WithIFormatProviderAndMultiplePlaceholders_ShouldNotReportDiagnostic() + { + await CreateProjectBuilder() + .WithSourceCode(""" + using System; + using System.Globalization; + + class Program + { + private static string DebuggerDisplay => string.Format(CultureInfo.InvariantCulture, "Column: {0}, Value: {1}, Invalid: {2}, Blank: {3}", "Column", "Value", true, false); + + static void Main(string[] args) + { + Console.WriteLine(DebuggerDisplay); + } + } + """) + .ValidateAsync(); + } + + [Fact] + public async Task StringFormat_WithExplicitEmptyParamsArray_ShouldReportDiagnostic() + { + await CreateProjectBuilder() + .WithSourceCode(""" + using System; + using System.Globalization; + + class Test + { + void Method() + { + var result = [|string.Format(CultureInfo.InvariantCulture, "no placeholders", new object[0])|]; + } + } + """) + .ValidateAsync(); + } }