Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preserve trivia when fixing CA1865 #6932

Merged
merged 1 commit into from
Sep 28, 2023
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 @@ -81,7 +81,7 @@ protected override void ApplyFix(
.Select(arg => arg!.Syntax));
var argumentListNode = SyntaxFactory.ArgumentList(SyntaxFactory.SeparatedList(arguments));

editor.ReplaceNode(oldArgumentListNode, argumentListNode);
editor.ReplaceNode(oldArgumentListNode, argumentListNode.WithTriviaFrom(oldArgumentListNode));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Testing;
using Test.Utilities;
using Xunit;
using VerifyCS = Test.Utilities.CSharpCodeFixVerifier<
Microsoft.CodeAnalysis.CSharp.NetAnalyzers.Microsoft.NetCore.Analyzers.Performance.CSharpUseStringMethodCharOverloadWithSingleCharacters,
Expand Down Expand Up @@ -119,6 +120,47 @@ await VerifyCSAsync(
});
}

[Fact]
[WorkItem(6930, "https://github.com/dotnet/roslyn-analyzers/issues/6930")]
public async Task CS_PreservesTrivia()
{
var testCode = $$"""
using System;

public class TestClass
{
public void TestMethod(string str)
{
if (str != "abc"
|| 2 == str.IndexOf{|CA1865:(".", StringComparison.Ordinal)|}
|| str == "test")
{
return;
}
}
}
""";

var fixedCode = $$"""
using System;

public class TestClass
{
public void TestMethod(string str)
{
if (str != "abc"
|| 2 == str.IndexOf('.')
|| str == "test")
{
return;
}
}
}
""";

await VerifyCSAsync(testCode, ReferenceAssemblies.NetStandard.NetStandard21, fixedCode);
}

[Theory]
[MemberData(nameof(IndexOfLastIndexOfMethods))]
public async Task CS_IndexOfLastIndexOf_PreservesStartsIndexAndCount(string method)
Expand Down Expand Up @@ -346,6 +388,41 @@ End Class
await VerifyVBAsync(testCode, ReferenceAssemblies.NetStandard.NetStandard21);
}

[Fact]
[WorkItem(6930, "https://github.com/dotnet/roslyn-analyzers/issues/6930")]
public async Task VB_PreservesTrivia()
{
var testCode = $$"""
Imports System

Public Class TestClass
Public Sub TestMethod(str As String)
If str <> "abc" _
Or 2 = str.IndexOf{|CA1865:(".", StringComparison.Ordinal)|} _
Or str = "test"
Return
End If
End Sub
End Class
""";

var fixedCode = $$"""
Imports System

Public Class TestClass
Public Sub TestMethod(str As String)
If str <> "abc" _
Or 2 = str.IndexOf("."c) _
Or str = "test"
Return
End If
End Sub
End Class
""";

await VerifyVBAsync(testCode, ReferenceAssemblies.NetStandard.NetStandard21, fixedCode);
}

[Theory]
[MemberData(nameof(Methods))]
public async Task VB_RegularStringLiteral(string method)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ Namespace Microsoft.NetCore.VisualBasic.Analyzers.Performance
[Select](Function(arg) TryCast(model.GetOperation(arg), IArgumentOperation)).Where(Function(arg) PreserveArgument(arg)).[Select](Function(arg) arg.Syntax))
Dim argumentListNode = SyntaxFactory.ArgumentList(SyntaxFactory.SeparatedList(arguments))

editor.ReplaceNode(oldArgumentListNode, argumentListNode)
editor.ReplaceNode(oldArgumentListNode, argumentListNode.WithTriviaFrom(oldArgumentListNode))
End Sub
End Class
End Class
Expand Down