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
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}