Skip to content

Commit 42babfd

Browse files
committed
Fixed analysis of Switch/Map with context.
1 parent 1f2bbe7 commit 42babfd

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

src/Thinktecture.Runtime.Extensions.SourceGenerator/CodeAnalysis/Diagnostics/ThinktectureRuntimeExtensionsAnalyzer.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ private static void AnalyzeMethodCall(OperationAnalysisContext context)
6060

6161
if (operation.Instance is null
6262
|| operation.Arguments.IsDefaultOrEmpty
63-
|| operation.Arguments.Length % 2 != 0
6463
|| operation.TargetMethod.IsStatic
6564
|| (operation.TargetMethod.Name != "Switch" && operation.TargetMethod.Name != "Map"))
6665
{
@@ -74,18 +73,19 @@ private static void AnalyzeMethodCall(OperationAnalysisContext context)
7473

7574
var nonIgnoredMembers = operation.Instance.Type.GetNonIgnoredMembers();
7675
var items = operation.Instance.Type.GetEnumItems(nonIgnoredMembers);
76+
var argsIndex = operation.TargetMethod.Parameters.Length % 2;
77+
var args = operation.Arguments;
7778

7879
for (var itemIndex = 0; itemIndex < items.Length; itemIndex++)
7980
{
8081
var item = items[itemIndex];
81-
var args = operation.Arguments;
8282

8383
if (args.IsDefaultOrEmpty)
8484
continue;
8585

8686
var found = false;
8787

88-
for (var argIndex = 0; argIndex < args.Length; argIndex += 2)
88+
for (var argIndex = argsIndex; argIndex < args.Length; argIndex += 2)
8989
{
9090
var argument = args[argIndex];
9191

test/Thinktecture.Runtime.Extensions.SourceGenerator.Tests/AnalyzerAndCodeFixTests/TTRESG039_SwitchMustCoverAllItems.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,35 @@ public void Do()
3737
await Verifier.VerifyAnalyzerAsync(code, new[] { typeof(ComplexValueObjectAttribute).Assembly, typeof(TestEnum).Assembly }, expected);
3838
}
3939

40+
[Fact]
41+
public async Task Should_trigger_on_Switch_missing_items_having_action_with_context()
42+
{
43+
var code = """
44+
45+
using System;
46+
using Thinktecture;
47+
using Thinktecture.Runtime.Tests.TestEnums;
48+
49+
namespace TestNamespace
50+
{
51+
public class Test
52+
{
53+
public void Do()
54+
{
55+
var testEnum = TestEnum.Item1;
56+
57+
{|#0:testEnum.Switch(42,
58+
TestEnum.Item1, value => {},
59+
TestEnum.Item1, value => {})|};
60+
}
61+
}
62+
}
63+
""";
64+
65+
var expected = Verifier.Diagnostic(_DIAGNOSTIC_ID).WithLocation(0).WithArguments("TestEnum", "Item2");
66+
await Verifier.VerifyAnalyzerAsync(code, new[] { typeof(ComplexValueObjectAttribute).Assembly, typeof(TestEnum).Assembly }, expected);
67+
}
68+
4069
[Fact]
4170
public async Task Should_not_trigger_on_Switch_when_all_items_are_covered_having_action()
4271
{

0 commit comments

Comments
 (0)