-
Notifications
You must be signed in to change notification settings - Fork 4k
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
Handle patterns in populate switch expressions #50984
Changes from all commits
79fc635
708274a
989d4e0
bc4847a
c39956f
8fe4efd
ec7d69f
39a4ad5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -962,7 +962,7 @@ void Method() | |
(MyEnum)0 => 1, | ||
(MyEnum)1 => 2, | ||
""Mismatching constant"" => 3, | ||
_ => throw new System.NotImplementedException(), | ||
MyEnum.FizzBuzz => throw new System.NotImplementedException(), | ||
} | ||
} | ||
}"); | ||
|
@@ -1040,7 +1040,7 @@ void Main() | |
Bar.Option1 => 1, | ||
Bar.Option2 => 2, | ||
null => null, | ||
_ => throw new System.NotImplementedException(), | ||
Bar.Option3 => throw new System.NotImplementedException(), | ||
Comment on lines
1041
to
+1043
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test is misleading. It's named There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. dont' change existing tests. you can rename them and/or add new tests. thanks! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @CyrusNajmabadi Well, I'll leave the test case and test name as-is for now. Only the expected result is updated. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. wfm. feel free to update the name as well if you want. but genrally don't make the test fit the name, make the name fit the test. and we feel there is a test hole, where no test checks the original name's case, then we can add that :) |
||
}; | ||
} | ||
|
||
|
@@ -1053,5 +1053,213 @@ public enum Bar | |
} | ||
"); | ||
} | ||
|
||
[Fact] | ||
[WorkItem(50982, "https://github.com/dotnet/roslyn/issues/50982")] | ||
public async Task TestOrPatternIsHandled() | ||
{ | ||
await TestInRegularAndScript1Async( | ||
@"public static class C | ||
{ | ||
static bool IsValidValue(E e) | ||
{ | ||
return e [||]switch | ||
{ | ||
E.A or E.B or E.C => true, | ||
_ = false | ||
}; | ||
} | ||
|
||
public enum E | ||
{ | ||
A, | ||
B, | ||
C, | ||
D, | ||
E, | ||
F, | ||
G, | ||
} | ||
} | ||
", | ||
@"public static class C | ||
{ | ||
static bool IsValidValue(E e) | ||
{ | ||
return e [||]switch | ||
{ | ||
E.A or E.B or E.C => true, | ||
E.D => throw new System.NotImplementedException(), | ||
E.E => throw new System.NotImplementedException(), | ||
E.F => throw new System.NotImplementedException(), | ||
E.G => throw new System.NotImplementedException(), | ||
_ = false | ||
}; | ||
} | ||
|
||
public enum E | ||
{ | ||
A, | ||
B, | ||
C, | ||
D, | ||
E, | ||
F, | ||
G, | ||
} | ||
} | ||
"); | ||
} | ||
|
||
[Fact] | ||
[WorkItem(50982, "https://github.com/dotnet/roslyn/issues/50982")] | ||
public async Task TestOrPatternIsHandled_AllEnumValuesAreHandled_NoDiagnostic() | ||
{ | ||
await TestMissingInRegularAndScriptAsync( | ||
@"public static class C | ||
{ | ||
static bool IsValidValue(E e) | ||
{ | ||
return e [||]switch | ||
{ | ||
(E.A or E.B) or (E.C or E.D) => true, | ||
(E.E or E.F) or (E.G) => true, | ||
_ = false | ||
}; | ||
} | ||
|
||
public enum E | ||
{ | ||
A, | ||
B, | ||
C, | ||
D, | ||
E, | ||
F, | ||
G, | ||
} | ||
} | ||
"); | ||
} | ||
|
||
[Fact] | ||
[WorkItem(50982, "https://github.com/dotnet/roslyn/issues/50982")] | ||
public async Task TestMixingOrWithAndPatterns() | ||
{ | ||
await TestInRegularAndScript1Async( | ||
@"public static class C | ||
{ | ||
static bool M(E e) | ||
{ | ||
return e [||]switch | ||
{ | ||
(E.A or E.B) and (E.C or E.D) => true, | ||
_ = false | ||
}; | ||
} | ||
|
||
public enum E | ||
{ | ||
A, | ||
B, | ||
C, | ||
D, | ||
E, | ||
F, | ||
G, | ||
} | ||
} | ||
", | ||
@"public static class C | ||
{ | ||
static bool M(E e) | ||
{ | ||
return e [||]switch | ||
{ | ||
(E.A or E.B) and (E.C or E.D) => true, | ||
E.A => throw new System.NotImplementedException(), | ||
E.B => throw new System.NotImplementedException(), | ||
E.C => throw new System.NotImplementedException(), | ||
E.D => throw new System.NotImplementedException(), | ||
E.E => throw new System.NotImplementedException(), | ||
E.F => throw new System.NotImplementedException(), | ||
E.G => throw new System.NotImplementedException(), | ||
_ = false | ||
}; | ||
} | ||
|
||
public enum E | ||
{ | ||
A, | ||
B, | ||
C, | ||
D, | ||
E, | ||
F, | ||
G, | ||
} | ||
} | ||
" | ||
); | ||
} | ||
|
||
[Fact] | ||
[WorkItem(50982, "https://github.com/dotnet/roslyn/issues/50982")] | ||
public async Task TestMixingOrWithAndPatterns2() | ||
{ | ||
await TestInRegularAndScript1Async( | ||
@"public static class C | ||
{ | ||
static bool M(E e) | ||
{ | ||
return e [||]switch | ||
{ | ||
(E.A or E.B) or (E.C and E.D) => true, | ||
_ = false | ||
}; | ||
} | ||
|
||
public enum E | ||
{ | ||
A, | ||
B, | ||
C, | ||
D, | ||
E, | ||
F, | ||
G, | ||
} | ||
} | ||
", | ||
@"public static class C | ||
{ | ||
static bool M(E e) | ||
{ | ||
return e [||]switch | ||
{ | ||
(E.A or E.B) or (E.C and E.D) => true, | ||
E.C => throw new System.NotImplementedException(), | ||
E.D => throw new System.NotImplementedException(), | ||
E.E => throw new System.NotImplementedException(), | ||
E.F => throw new System.NotImplementedException(), | ||
E.G => throw new System.NotImplementedException(), | ||
_ = false | ||
}; | ||
} | ||
|
||
public enum E | ||
{ | ||
A, | ||
B, | ||
C, | ||
D, | ||
E, | ||
F, | ||
G, | ||
} | ||
} | ||
" | ||
); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When "Mismatching constant" was encountered, the analyzer considered the switch as "complete". Hence, it failed to 'add missing cases'.
The "mismatching constant" is a compile-error anyway. So it probably doesn't matter a lot.