IDE0066 (switch pattern) code fix is incorrect
See original GitHub issueVersion Used: 16.8.0 Preview 3.0
Steps to Reproduce:
The following snippet
switch (invocation.TargetMethod.Name)
{
case "Last":
case "First":
case "Count":
return true;
default:
return false;
}
gets refactored into the following form:
return invocation.TargetMethod.Name switch
{
"Last" => true,
_ => false,
};
which is incorrect.
Expected Behavior:
All cases are correctly converted.
Actual Behavior:
The code fix only take the first case declared.
Issue Analytics
- State:
- Created 3 years ago
- Comments:7 (7 by maintainers)
Top Results From Across the Web
IDE0066 Suggests code that doesn't compile for implicitly ...
So the switch expression's input type is C . The constant pattern "A" is converted to the input type, in this case using...
Read more >IDE0066: Use switch expression - .NET
In this article ; Title, Use switch expression ; Category, Style ; Subcategory, Language rules (pattern matching preferences) ; Applicable languages ...
Read more >c# - Why does Visual Studio 2019 recommend a switch ...
If we used a switch statement we'd have to say something like this: string description = null; switch(reason) { case Reasons.
Read more >Untitled
I'd recommend updating your switch statement example to use the discard pattern for the cases: case Type1 _:, case Type2 _: etc. crepid...
Read more >Untitled
Patterns - Pattern matching using the is and switch expressions. if and switch statements - select a code path to execute Web21 thg...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Doesn’t repro for me either. Likely fixed at some point. Have created test to validate this is workign and will not regress.
It was originally excluded rather than use lightup:
https://github.com/dotnet/roslyn/blob/85601f9301e2790ed23ec189412e28b60b48d838/src/Analyzers/CSharp/CodeFixes/ConvertSwitchStatementToExpression/ConvertSwitchStatementToExpressionCodeFixProvider.Rewriter.cs#L132-L142
Fixed by #46873