Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
@@ -1,4 +1,4 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Collections.Immutable;
Expand Down Expand Up @@ -51,37 +51,32 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
context.RegisterCodeFix(
CodeAction.Create(
title: CodeFixResources.AvoidOutRefTestMethodParametersFix,
createChangedSolution: c => RemoveOutRefModifiersAsync(context.Document, methodDeclaration, c),
createChangedDocument: c => RemoveOutRefModifiersAsync(context.Document, methodDeclaration, c),
equivalenceKey: nameof(AvoidOutRefTestMethodParametersFixer)),
diagnostic);
}

private static async Task<Solution> RemoveOutRefModifiersAsync(Document document, MethodDeclarationSyntax methodDeclaration, CancellationToken cancellationToken)
private static async Task<Document> RemoveOutRefModifiersAsync(Document document, MethodDeclarationSyntax methodDeclaration, CancellationToken cancellationToken)
{
DocumentEditor editor = await DocumentEditor.CreateAsync(document, cancellationToken).ConfigureAwait(false);

// Remove out/ref modifiers from parameters
SeparatedSyntaxList<ParameterSyntax> newParameters = methodDeclaration.ParameterList.Parameters;

for (int i = 0; i < newParameters.Count; i++)
foreach (ParameterSyntax parameter in methodDeclaration.ParameterList.Parameters)
{
ParameterSyntax parameter = newParameters[i];
SyntaxTokenList filteredModifiers = SyntaxFactory.TokenList(
parameter.Modifiers.Where(m => !m.IsKind(SyntaxKind.OutKeyword) && !m.IsKind(SyntaxKind.RefKeyword)));
int indexToRemove = parameter.Modifiers.IndexOf(SyntaxKind.OutKeyword);
if (indexToRemove < 0)
{
indexToRemove = parameter.Modifiers.IndexOf(SyntaxKind.RefKeyword);
}

if (filteredModifiers.Count != parameter.Modifiers.Count)
if (indexToRemove >= 0)
{
ParameterSyntax newParameter = parameter.WithModifiers(filteredModifiers);
newParameters = newParameters.Replace(parameter, newParameter);
editor.ReplaceNode(parameter, (node, _) =>
{
var parameter = (ParameterSyntax)node;
return parameter.WithModifiers(parameter.Modifiers.RemoveAt(indexToRemove));
});
}
}

MethodDeclarationSyntax newMethodDeclaration = methodDeclaration.WithParameterList(
methodDeclaration.ParameterList.WithParameters(newParameters));

editor.ReplaceNode(methodDeclaration, newMethodDeclaration);

Document newDocument = editor.GetChangedDocument();
return newDocument.Project.Solution;
return editor.GetChangedDocument();
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using VerifyCS = MSTest.Analyzers.Test.CSharpCodeFixVerifier<
Expand Down Expand Up @@ -116,6 +116,46 @@ public void TestMethod1(string s, string s2)
await VerifyCS.VerifyCodeFixAsync(code, fixedCode);
}

[TestMethod]
public async Task WhenTestMethodHasOutAndRefParametersOnMultiLine_Diagnostic()
{
string code = """
using Microsoft.VisualStudio.TestTools.UnitTesting;

[TestClass]
public class MyTestClass
{
[TestMethod]
[DataRow("Hello", "World")]
public void [|TestMethod1|](
out string s,
ref string s2)
{
s = "";
}
}
""";

string fixedCode = """
using Microsoft.VisualStudio.TestTools.UnitTesting;

[TestClass]
public class MyTestClass
{
[TestMethod]
[DataRow("Hello", "World")]
public void TestMethod1(
string s,
string s2)
{
s = "";
}
}
""";

await VerifyCS.VerifyCodeFixAsync(code, fixedCode);
}

[TestMethod]
public async Task WhenTestMethodHasNormalParameters_NoDiagnostic()
{
Expand Down
Loading