Skip to content

Commit cc22566

Browse files
Cover fix-all for CA1862, CA1866 and CA2250
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
1 parent c3cfcdb commit cc22566

4 files changed

Lines changed: 206 additions & 0 deletions

File tree

src/Microsoft.CodeAnalysis.NetAnalyzers/tests/Microsoft.CodeAnalysis.NetAnalyzers.UnitTests/Microsoft.NetCore.Analyzers/Performance/RecommendCaseInsensitiveStringComparison.CSharp.Tests.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,44 @@ void M(string s)
432432
}.RunAsync(CancellationToken.None);
433433
}
434434

435+
[TestMethod]
436+
public Task NestedDiagnostics_CSharp_FixAllRewritesBoth()
437+
{
438+
string originalCode = @"using System;
439+
class C
440+
{
441+
void M()
442+
{
443+
string a = ""aBc"";
444+
string b = ""bc"";
445+
string c = ""c"";
446+
var result = a.ToLower().StartsWith(b.ToLower().IndexOf(c) > 0 ? ""x"" : ""y"");
447+
}
448+
}";
449+
string fixedCode = @"using System;
450+
class C
451+
{
452+
void M()
453+
{
454+
string a = ""aBc"";
455+
string b = ""bc"";
456+
string c = ""c"";
457+
var result = a.StartsWith(b.IndexOf(c, StringComparison.CurrentCultureIgnoreCase) > 0 ? ""x"" : ""y"", StringComparison.CurrentCultureIgnoreCase);
458+
}
459+
}";
460+
return new VerifyCS.Test
461+
{
462+
TestCode = originalCode,
463+
FixedCode = fixedCode,
464+
ReferenceAssemblies = ReferenceAssemblies.NetFramework.Net48.Default,
465+
ExpectedDiagnostics =
466+
{
467+
VerifyCS.Diagnostic(RecommendCaseInsensitiveStringComparisonAnalyzer.RecommendCaseInsensitiveStringComparisonRule).WithSpan(9, 22, 9, 84).WithArguments("string.StartsWith(string)"),
468+
VerifyCS.Diagnostic(RecommendCaseInsensitiveStringComparisonAnalyzer.RecommendCaseInsensitiveStringComparisonRule).WithSpan(9, 45, 9, 67).WithArguments("string.IndexOf(string)")
469+
}
470+
}.RunAsync(CancellationToken.None);
471+
}
472+
435473
private async Task VerifyNoDiagnosticCSharpAsync(string originalSource)
436474
{
437475
VerifyCS.Test test = new()

src/Microsoft.CodeAnalysis.NetAnalyzers/tests/Microsoft.CodeAnalysis.NetAnalyzers.UnitTests/Microsoft.NetCore.Analyzers/Performance/RecommendCaseInsensitiveStringComparison.VisualBasic.Tests.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,42 @@ private async Task VerifyNoDiagnosticVisualBasicAsync(string originalSource)
391391
await test.RunAsync(CancellationToken.None);
392392
}
393393

394+
[TestMethod]
395+
public Task NestedDiagnostics_VisualBasic_FixAllRewritesBoth()
396+
{
397+
string originalCode = @"Imports System
398+
Class C
399+
Private Sub M()
400+
Dim a As String = ""aBc""
401+
Dim b As String = ""bc""
402+
Dim c As String = ""c""
403+
Dim result = a.ToLower().StartsWith(If(b.ToLower().IndexOf(c) > 0, ""x"", ""y""))
404+
End Sub
405+
End Class
406+
";
407+
string fixedCode = @"Imports System
408+
Class C
409+
Private Sub M()
410+
Dim a As String = ""aBc""
411+
Dim b As String = ""bc""
412+
Dim c As String = ""c""
413+
Dim result = a.StartsWith(If(b.IndexOf(c, StringComparison.CurrentCultureIgnoreCase) > 0, ""x"", ""y""), StringComparison.CurrentCultureIgnoreCase)
414+
End Sub
415+
End Class
416+
";
417+
return new VerifyVB.Test
418+
{
419+
TestCode = originalCode,
420+
FixedCode = fixedCode,
421+
ReferenceAssemblies = ReferenceAssemblies.NetFramework.Net48.Default,
422+
ExpectedDiagnostics =
423+
{
424+
VerifyVB.Diagnostic(RecommendCaseInsensitiveStringComparisonAnalyzer.RecommendCaseInsensitiveStringComparisonRule).WithSpan(7, 22, 7, 86).WithArguments("Public Overloads Function StartsWith(value As String) As Boolean"),
425+
VerifyVB.Diagnostic(RecommendCaseInsensitiveStringComparisonAnalyzer.RecommendCaseInsensitiveStringComparisonRule).WithSpan(7, 48, 7, 70).WithArguments("Public Overloads Function IndexOf(value As String) As Integer")
426+
}
427+
}.RunAsync(CancellationToken.None);
428+
}
429+
394430
private async Task VerifyFixVisualBasicAsync(string originalSource, string fixedSource)
395431
{
396432
VerifyVB.Test test = new()

src/Microsoft.CodeAnalysis.NetAnalyzers/tests/Microsoft.CodeAnalysis.NetAnalyzers.UnitTests/Microsoft.NetCore.Analyzers/Runtime/PreferAsSpanOverSubstringTests.cs

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -817,6 +817,82 @@ public static void Consume(Roschar span) { }
817817
return test.RunAsync(CancellationToken.None);
818818
}
819819

820+
[TestMethod]
821+
public Task SystemNamespace_IsAddedOnce_WhenTwoViolationsAreFixed_CSAsync()
822+
{
823+
string receiver = CS.Usings + @"
824+
public class C
825+
{
826+
public static void Consume(string text) { }
827+
public static void Consume(Roschar span) { }
828+
}";
829+
string testCode = CS.WithBody(
830+
WithKey(@"C.Consume(foo.Substring(1))", 0) + ';' + Environment.NewLine +
831+
WithKey(@"C.Consume(foo.Substring(2))", 1) + ';',
832+
includeUsings: false);
833+
string fixedCode = CS.WithBody(
834+
@"C.Consume(foo.AsSpan(1));" + Environment.NewLine +
835+
@"C.Consume(foo.AsSpan(2));",
836+
includeUsings: true);
837+
838+
var test = new VerifyCS.Test
839+
{
840+
TestState =
841+
{
842+
Sources = { testCode, receiver },
843+
ExpectedDiagnostics = { CS.DiagnosticAt(0), CS.DiagnosticAt(1) }
844+
},
845+
FixedState =
846+
{
847+
Sources = { fixedCode, receiver }
848+
},
849+
ReferenceAssemblies = ReferenceAssemblies.Net.Net50
850+
};
851+
return test.RunAsync(CancellationToken.None);
852+
}
853+
854+
[TestMethod]
855+
public Task SystemNamespace_IsAddedOnce_WhenTwoViolationsAreFixed_VBAsync()
856+
{
857+
string receiver = CS.Usings + @"
858+
public class C
859+
{
860+
public static void Consume(string text) { }
861+
public static void Consume(Roschar span) { }
862+
}";
863+
string testCode = VB.WithBody(
864+
WithKey(@"C.Consume(foo.Substring(1))", 0) + Environment.NewLine +
865+
WithKey(@"C.Consume(foo.Substring(2))", 1),
866+
includeImports: false);
867+
string fixedCode = VB.WithBody(
868+
@"C.Consume(foo.AsSpan(1))" + Environment.NewLine +
869+
@"C.Consume(foo.AsSpan(2))",
870+
includeImports: true);
871+
var receiverProject = new ProjectState("Receiver", LanguageNames.CSharp, "receiver", "cs")
872+
{
873+
Sources = { receiver }
874+
};
875+
876+
var test = new VerifyVB.Test
877+
{
878+
TestState =
879+
{
880+
Sources = { testCode },
881+
AdditionalProjects = { { receiverProject.Name, receiverProject } },
882+
AdditionalProjectReferences = { receiverProject.Name },
883+
ExpectedDiagnostics = { VB.DiagnosticAt(0), VB.DiagnosticAt(1) }
884+
},
885+
FixedState =
886+
{
887+
Sources = { fixedCode },
888+
AdditionalProjects = { { receiverProject.Name, receiverProject } },
889+
AdditionalProjectReferences = { receiverProject.Name }
890+
},
891+
ReferenceAssemblies = ReferenceAssemblies.Net.Net50
892+
};
893+
return test.RunAsync(CancellationToken.None);
894+
}
895+
820896
[TestMethod]
821897
public Task SystemNamespace_IsNotAdded_WhenIncludedGlobally_VBAsync()
822898
{

src/Microsoft.CodeAnalysis.NetAnalyzers/tests/Microsoft.CodeAnalysis.NetAnalyzers.UnitTests/Microsoft.NetCore.Analyzers/Runtime/UseCancellationTokenThrowIfCancellationRequestedTests.cs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,62 @@ public Task SimpleAffirmativeCheck_ReportedAndFixed_CSAsync(string operationCanc
8383
return test.RunAsync(CancellationToken.None);
8484
}
8585

86+
[TestMethod]
87+
public Task NestedChecks_AreAllReportedAndFixed_CSAsync()
88+
{
89+
string testStatements = @"{|#0:if (token.IsCancellationRequested)
90+
throw new OperationCanceledException();
91+
else
92+
{
93+
{|#1:if (token.IsCancellationRequested)
94+
throw new OperationCanceledException();|}
95+
Console.WriteLine();
96+
}|}";
97+
string fixedStatements = @"token.ThrowIfCancellationRequested();
98+
token.ThrowIfCancellationRequested();
99+
Console.WriteLine();";
100+
101+
var test = new VerifyCS.Test
102+
{
103+
TestCode = CS.CreateBlock(testStatements),
104+
FixedCode = CS.CreateBlock(fixedStatements),
105+
ExpectedDiagnostics = { CS.DiagnosticAt(0), CS.DiagnosticAt(1) },
106+
ReferenceAssemblies = ReferenceAssemblies.Net.Net50,
107+
// The outer fix rewrites the region the inner one sits in, so whichever provider applies them
108+
// has to discard one and pick it up on a second pass.
109+
NumberOfFixAllIterations = 2,
110+
};
111+
return test.RunAsync(CancellationToken.None);
112+
}
113+
114+
[TestMethod]
115+
public Task NestedChecks_AreAllReportedAndFixed_VBAsync()
116+
{
117+
string testStatements = @"{|#0:If token.IsCancellationRequested Then
118+
Throw New OperationCanceledException()
119+
Else
120+
{|#1:If token.IsCancellationRequested Then
121+
Throw New OperationCanceledException()
122+
End If|}
123+
Console.WriteLine()
124+
End If|}";
125+
string fixedStatements = @"token.ThrowIfCancellationRequested()
126+
token.ThrowIfCancellationRequested()
127+
Console.WriteLine()";
128+
129+
var test = new VerifyVB.Test
130+
{
131+
TestCode = VB.CreateBlock(testStatements),
132+
FixedCode = VB.CreateBlock(fixedStatements),
133+
ExpectedDiagnostics = { VB.DiagnosticAt(0), VB.DiagnosticAt(1) },
134+
ReferenceAssemblies = ReferenceAssemblies.Net.Net50,
135+
// The outer fix rewrites the region the inner one sits in, so whichever provider applies them
136+
// has to discard one and pick it up on a second pass.
137+
NumberOfFixAllIterations = 2,
138+
};
139+
return test.RunAsync(CancellationToken.None);
140+
}
141+
86142
public static IEnumerable<object[]> Data_SimpleAffirmativeCheck_ReportedAndFixed_VB
87143
{
88144
get

0 commit comments

Comments
 (0)