Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitfdef2db

Browse files
committed
Preserve trivia when fixing CA1865
1 parentc8647c0 commitfdef2db

File tree

3 files changed

+79
-2
lines changed

3 files changed

+79
-2
lines changed

‎src/NetAnalyzers/CSharp/Microsoft.NetCore.Analyzers/Performance/CSharpUseStringMethodCharOverloadWithSingleCharacters.Fixer.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ protected override void ApplyFix(
8181
.Select(arg=>arg!.Syntax));
8282
varargumentListNode=SyntaxFactory.ArgumentList(SyntaxFactory.SeparatedList(arguments));
8383

84-
editor.ReplaceNode(oldArgumentListNode,argumentListNode);
84+
editor.ReplaceNode(oldArgumentListNode,argumentListNode.WithTriviaFrom(oldArgumentListNode));
8585
}
8686
}
8787
}

‎src/NetAnalyzers/UnitTests/Microsoft.NetCore.Analyzers/Performance/UseStringMethodCharOverloadWithSingleCharactersTests.cs‎

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
usingSystem.Threading.Tasks;
77
usingMicrosoft.CodeAnalysis;
88
usingMicrosoft.CodeAnalysis.Testing;
9+
usingTest.Utilities;
910
usingXunit;
1011
usingVerifyCS=Test.Utilities.CSharpCodeFixVerifier<
1112
Microsoft.CodeAnalysis.CSharp.NetAnalyzers.Microsoft.NetCore.Analyzers.Performance.CSharpUseStringMethodCharOverloadWithSingleCharacters,
@@ -119,6 +120,47 @@ await VerifyCSAsync(
119120
});
120121
}
121122

123+
[Fact]
124+
[WorkItem(6930,"https://github.com/dotnet/roslyn-analyzers/issues/6930")]
125+
publicasyncTaskCS_PreservesTrivia()
126+
{
127+
vartestCode=$$"""
128+
using System;
129+
130+
public class TestClass
131+
{
132+
public void TestMethod(string str)
133+
{
134+
if (str != "abc"
135+
|| 2 == str.IndexOf{|CA1865:(".", StringComparison.Ordinal)|}
136+
|| str == "test")
137+
{
138+
return;
139+
}
140+
}
141+
}
142+
""";
143+
144+
varfixedCode=$$"""
145+
using System;
146+
147+
public class TestClass
148+
{
149+
public void TestMethod(string str)
150+
{
151+
if (str != "abc"
152+
|| 2 == str.IndexOf('.')
153+
|| str == "test")
154+
{
155+
return;
156+
}
157+
}
158+
}
159+
""";
160+
161+
awaitVerifyCSAsync(testCode,ReferenceAssemblies.NetStandard.NetStandard21,fixedCode);
162+
}
163+
122164
[Theory]
123165
[MemberData(nameof(IndexOfLastIndexOfMethods))]
124166
publicasyncTaskCS_IndexOfLastIndexOf_PreservesStartsIndexAndCount(stringmethod)
@@ -346,6 +388,41 @@ End Class
346388
awaitVerifyVBAsync(testCode,ReferenceAssemblies.NetStandard.NetStandard21);
347389
}
348390

391+
[Fact]
392+
[WorkItem(6930,"https://github.com/dotnet/roslyn-analyzers/issues/6930")]
393+
publicasyncTaskVB_PreservesTrivia()
394+
{
395+
vartestCode=$$"""
396+
Imports System
397+
398+
Public Class TestClass
399+
Public Sub TestMethod(str As String)
400+
If str <> "abc" _
401+
Or 2 = str.IndexOf{|CA1865:(".", StringComparison.Ordinal)|} _
402+
Or str = "test"
403+
Return
404+
End If
405+
End Sub
406+
End Class
407+
""";
408+
409+
varfixedCode=$$"""
410+
Imports System
411+
412+
Public Class TestClass
413+
Public Sub TestMethod(str As String)
414+
If str <> "abc" _
415+
Or 2 = str.IndexOf("."c) _
416+
Or str = "test"
417+
Return
418+
End If
419+
End Sub
420+
End Class
421+
""";
422+
423+
awaitVerifyVBAsync(testCode,ReferenceAssemblies.NetStandard.NetStandard21,fixedCode);
424+
}
425+
349426
[Theory]
350427
[MemberData(nameof(Methods))]
351428
publicasyncTaskVB_RegularStringLiteral(stringmethod)

‎src/NetAnalyzers/VisualBasic/Microsoft.NetCore.Analyzers/Performance/BasicUseStringMethodCharOverloadWithSingleCharacters.Fixer.vb‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ Namespace Microsoft.NetCore.VisualBasic.Analyzers.Performance
7171
[Select](Function(arg)TryCast(model.GetOperation(arg),IArgumentOperation)).Where(Function(arg)PreserveArgument(arg)).[Select](Function(arg)arg.Syntax))
7272
DimargumentListNode=SyntaxFactory.ArgumentList(SyntaxFactory.SeparatedList(arguments))
7373

74-
editor.ReplaceNode(oldArgumentListNode,argumentListNode)
74+
editor.ReplaceNode(oldArgumentListNode,argumentListNode.WithTriviaFrom(oldArgumentListNode))
7575
EndSub
7676
EndClass
7777
EndClass

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp