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 @@ -8,6 +8,7 @@
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Editing;
using Microsoft.CodeAnalysis.Operations;
using Microsoft.CodeAnalysis.Simplification;

namespace Meziantou.Analyzer.Rules;

Expand Down Expand Up @@ -253,8 +254,13 @@ private static async Task<Document> ReplaceStringFormatWithAppendFormat(Document

var stringFormatOperation = (IInvocationOperation)operation.Arguments[0].Value;

var stringFormatSyntax = (InvocationExpressionSyntax)stringFormatOperation.Syntax;
var arguments = stringFormatSyntax.ArgumentList.Arguments
.Select(static argument => (SyntaxNode)argument.WithAdditionalAnnotations(Simplifier.Annotation))
.ToArray();

var newExpression = generator.InvocationExpression(generator.MemberAccessExpression(operation.GetChildOperations().First().Syntax, "AppendFormat"),
[.. stringFormatOperation.Arguments.Select(a => a.Syntax)]);
[.. arguments]);

if (isAppendLine)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,39 @@ void A()
.ValidateAsync();
}

[Fact]
public async Task AppendLine_AppendFormat_ImplicitParamsArray()
{
await CreateProjectBuilder()
.WithSourceCode(
"""
using System.Globalization;
using System.Text;

class Test
{
void A()
{
[||]new StringBuilder().AppendLine(string.Format(CultureInfo.InvariantCulture, "{0} {1} {2} {3}", string.Empty, "MilliSec", "%", "Comment"));
}
}
""")
.ShouldFixCodeWith(
"""
using System.Globalization;
using System.Text;

class Test
{
void A()
{
new StringBuilder().AppendFormat(CultureInfo.InvariantCulture, "{0} {1} {2} {3}", string.Empty, "MilliSec", "%", "Comment").AppendLine();
}
}
""")
.ValidateAsync();
}

[Fact]
public async Task Append_StringJoin_AppendJoin_OldTargetFramework()
{
Expand Down