From cdaa559d8d2406938a41469c81c8c1a81c1c4849 Mon Sep 17 00:00:00 2001 From: campersau Date: Sat, 28 Sep 2024 19:56:32 +0200 Subject: [PATCH] Fix xUnit1051 move argument list creation inside of change document (#190) --- .../X1000/UseCancellationTokenFixer.cs | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/xunit.analyzers.fixes/X1000/UseCancellationTokenFixer.cs b/src/xunit.analyzers.fixes/X1000/UseCancellationTokenFixer.cs index dbf65955..c0561889 100644 --- a/src/xunit.analyzers.fixes/X1000/UseCancellationTokenFixer.cs +++ b/src/xunit.analyzers.fixes/X1000/UseCancellationTokenFixer.cs @@ -51,7 +51,7 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context) if (root.FindNode(diagnostic.Location.SourceSpan) is not InvocationExpressionSyntax invocation) return; - var arguments = new List(invocation.ArgumentList.Arguments); + var arguments = invocation.ArgumentList.Arguments; for (var argumentIndex = 0; argumentIndex < arguments.Count; argumentIndex++) { @@ -76,24 +76,26 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context) "CancellationToken" ); - if (parameterIndex < arguments.Count) + var args = new List(arguments); + + if (parameterIndex < args.Count) { - arguments[parameterIndex] = arguments[parameterIndex].WithExpression(testContextCancellationTokenExpression); + args[parameterIndex] = args[parameterIndex].WithExpression(testContextCancellationTokenExpression); } else { var argument = Argument(testContextCancellationTokenExpression); - if (parameterIndex > arguments.Count || arguments.Any(arg => arg.NameColon is not null)) + if (parameterIndex > args.Count || args.Any(arg => arg.NameColon is not null)) { argument = argument.WithNameColon(NameColon(parameterName)); } - arguments.Add(argument); + args.Add(argument); } editor.ReplaceNode( invocation, invocation - .WithArgumentList(ArgumentList(SeparatedList(arguments))) + .WithArgumentList(ArgumentList(SeparatedList(args))) ); return editor.GetChangedDocument();