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

Add support for or-pattern in IDE0010 #73574

Merged
merged 3 commits into from
May 25, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -1638,4 +1638,122 @@ void Method(int i)
}
""");
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/73245")]
public async Task NotAllMembersExist_NotDefault_WithOr()
{
await TestInRegularAndScriptAsync(
"""
namespace ConsoleApplication1
{
enum MyEnum
{
Fizz,
Buzz,
FizzBuzz
}

class MyClass
{
void Method()
{
var e = MyEnum.Fizz;
[||]switch (e)
{
case MyEnum.Fizz or MyEnum.Buzz:
break;
}
}
}
}
""",
"""
namespace ConsoleApplication1
{
enum MyEnum
{
Fizz,
Buzz,
FizzBuzz
}

class MyClass
{
void Method()
{
var e = MyEnum.Fizz;
switch (e)
{
case MyEnum.Fizz or MyEnum.Buzz:
break;
case MyEnum.FizzBuzz:
break;
default:
break;
}
}
}
}
""", index: 2);
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/73245")]
public async Task NotAllMembersExist_WithDefault_WithOr()
{
await TestInRegularAndScriptAsync(
"""
namespace ConsoleApplication1
{
enum MyEnum
{
Fizz,
Buzz,
FizzBuzz
}

class MyClass
{
void Method()
{
var e = MyEnum.Fizz;
[||]switch (e)
{
case MyEnum.Fizz or MyEnum.Buzz:
break;
default:
break;
}
}
}
}
""",
"""
namespace ConsoleApplication1
{
enum MyEnum
{
Fizz,
Buzz,
FizzBuzz
}

class MyClass
{
void Method()
{
var e = MyEnum.Fizz;
switch (e)
{
case MyEnum.Fizz or MyEnum.Buzz:
break;
case MyEnum.FizzBuzz:
break;
default:
break;
}
}
}
}
""");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,30 @@ private static bool TryRemoveExistingEnumMembers(ISwitchOperation switchStatemen
var caseValue = IntegerUtilities.ToInt64(value.ConstantValue.Value);
enumValues.Remove(caseValue);

break;

case CaseKind.Pattern:
foreach (var operation in clause.ChildOperations) // clause is IPatternCaseClauseOperation
{
foreach (var subCase in operation.ChildOperations) // operation is IBinaryPatternOperation
{
if (subCase.Kind is not OperationKind.ConstantPattern)
continue;

foreach (var subCaseOp in subCase.ChildOperations)
{
if (subCaseOp is not IFieldReferenceOperation field)
continue;

if (field.Field.ConstantValue is null)
continue;

var @case = IntegerUtilities.ToInt64(field.Field.ConstantValue);
enumValues.Remove(@case);
}
}
}

break;
}
}
Expand Down
Loading