diff --git a/src/Meziantou.Analyzer/Rules/StringFormatShouldBeConstantAnalyzer.cs b/src/Meziantou.Analyzer/Rules/StringFormatShouldBeConstantAnalyzer.cs index ec394a99..bcb1aa66 100644 --- a/src/Meziantou.Analyzer/Rules/StringFormatShouldBeConstantAnalyzer.cs +++ b/src/Meziantou.Analyzer/Rules/StringFormatShouldBeConstantAnalyzer.cs @@ -143,8 +143,8 @@ private static bool HasFormattingArguments(IInvocationOperation operation, int f } #endif - // Skip other implicit arguments - if (arg.IsImplicit) + // Skip arguments that were not explicitly provided (e.g. default values) + if (arg.ArgumentKind is ArgumentKind.DefaultValue) continue; return true; diff --git a/tests/Meziantou.Analyzer.Test/Rules/StringFormatShouldBeConstantAnalyzerTests.cs b/tests/Meziantou.Analyzer.Test/Rules/StringFormatShouldBeConstantAnalyzerTests.cs index 05b9eb6c..e976d3f4 100644 --- a/tests/Meziantou.Analyzer.Test/Rules/StringFormatShouldBeConstantAnalyzerTests.cs +++ b/tests/Meziantou.Analyzer.Test/Rules/StringFormatShouldBeConstantAnalyzerTests.cs @@ -734,4 +734,44 @@ void Method() """) .ValidateAsync(); } + + [Fact] + public async Task StringFormat_WithParenthesizedArgAndPlaceholder_ShouldNotReportDiagnostic() + { + await CreateProjectBuilder() + .WithSourceCode(""" + using System; + using System.Globalization; + + class Test + { + void Method(object obj) + { + _ = string.Format(CultureInfo.InvariantCulture, "Format string with placeholder: '{0}'.", (obj is null ? string.Empty : "X")); + } + } + """) + .WithTargetFramework(Helpers.TargetFramework.Net10_0) + .ValidateAsync(); + } + + [Fact] + public async Task StringFormat_WithParenthesizedArgAndNoPlaceholder_ShouldReportDiagnostic() + { + await CreateProjectBuilder() + .WithSourceCode(""" + using System; + using System.Globalization; + + class Test + { + void Method(object obj) + { + _ = [|string.Format(CultureInfo.InvariantCulture, "Format string without placeholder.", (obj is null ? string.Empty : "X"))|]; + } + } + """) + .WithTargetFramework(Helpers.TargetFramework.Net10_0) + .ValidateAsync(); + } }