未验证 提交 0106c3ab 编写于 作者: J Jason Malinowski 提交者: GitHub

Merge pull request #39465 from alrz/fix-if-to-switch

Fix minor issue in IfToSwitch refactoring
......@@ -70,6 +70,87 @@ void M(int i)
}");
}
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsConvertIfToSwitch)]
public async Task TestMissingOnSubsequentBlock()
{
await TestMissingInRegularAndScriptAsync(
@"class C
{
int M(int i)
{
[||]if (i == 3) return 0;
{ if (i == 6) return 1; }
return 2;
}
}");
}
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsConvertIfToSwitch)]
public async Task TestElseBlock_01()
{
await TestInRegularAndScriptAsync(
@"class C
{
int M(int i)
{
[||]if (i == 3) return 0;
else { if (i == 6) return 1; }
}
}",
@"class C
{
int M(int i)
{
switch (i)
{
case 3:
return 0;
case 6:
return 1;
}
}
}");
}
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsConvertIfToSwitch)]
public async Task TestElseBlock_02()
{
await TestInRegularAndScriptAsync(
@"class C
{
int M(int i)
{
[||]if (i == 3)
{
return 0;
}
else
{
if (i == 6) return 1;
if (i == 7) return 1;
return 0;
}
}
}",
@"class C
{
int M(int i)
{
switch (i)
{
case 3:
return 0;
case 6:
return 1;
case 7:
return 1;
default:
return 0;
}
}
}");
}
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsConvertIfToSwitch)]
public async Task TestMultipleCases_01()
{
......@@ -595,6 +676,33 @@ int M(int? i)
}", index: 1);
}
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsConvertIfToSwitch)]
public async Task TestSwitchExpression_02()
{
await TestInRegularAndScriptAsync(
@"class C
{
int M(int? i)
{
[||]if (i == null) { return 5; }
if (i == 0) { return 6; }
else { return 7; }
}
}",
@"class C
{
int M(int? i)
{
return i switch
{
null => 5,
0 => 6,
_ => 7
};
}
}", index: 1);
}
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsConvertIfToSwitch)]
public async Task TestSubsequentIfStatements_02()
{
......@@ -706,7 +814,7 @@ int M(int i)
[||]if (i == 10) return 5;
if (i == 20) return 6;
if (i == i) return 0;
reuturn 7;
return 7;
}
}",
@"class C
......@@ -721,7 +829,7 @@ int M(int i)
return 6;
}
if (i == i) return 0;
reuturn 7;
return 7;
}
}");
}
......@@ -746,7 +854,7 @@ int M(int i)
{
return 0;
}
reuturn 7;
return 7;
}
}",
@"class C
......@@ -764,7 +872,7 @@ int M(int i)
{
return 0;
}
reuturn 7;
return 7;
}
}");
}
......@@ -798,7 +906,7 @@ int M(int i)
{
return 0;
}
reuturn 7;
return 7;
}
}",
@"class C
......@@ -825,7 +933,7 @@ int M(int i)
{
return 0;
}
reuturn 7;
return 7;
}
}");
}
......
......@@ -26,9 +26,9 @@ internal abstract partial class AbstractConvertIfToSwitchCodeRefactoringProvider
//
// <if-statement>
// : if (<section-expr>) { _ } else <if-statement>
// | if (<section-expr>) { _ } else { <if-statement-sequence> }
// | if (<section-expr>) { _ } else { _ }
// | if (<section-expr>) { _ }
// | { <if-statement-sequence> }
//
// <section-expr>
// : <section-expr> || <pattern-expr>
......@@ -131,17 +131,14 @@ private bool ParseIfStatementSequence(ReadOnlySpan<IOperation> operations, Array
//
// <if-statement>
// : if (<section-expr>) { _ } else <if-statement>
// | if (<section-expr>) { _ } else { <if-statement-sequence> }
// | if (<section-expr>) { _ } else { _ }
// | if (<section-expr>) { _ }
// | { <if-statement-sequence> }
//
private bool ParseIfStatement(IOperation operation, ArrayBuilder<AnalyzedSwitchSection> sections, out IOperation? defaultBodyOpt)
{
switch (operation)
{
case IBlockOperation op:
return ParseIfStatementSequence(op.Operations.AsSpan(), sections, out defaultBodyOpt);
case IConditionalOperation op when CanConvert(op):
var section = ParseSwitchSection(op);
if (section is null)
......@@ -155,7 +152,7 @@ private bool ParseIfStatement(IOperation operation, ArrayBuilder<AnalyzedSwitchS
{
defaultBodyOpt = null;
}
else if (!ParseIfStatement(op.WhenFalse, sections, out defaultBodyOpt))
else if (!ParseIfStatementOrBlock(op.WhenFalse, sections, out defaultBodyOpt))
{
defaultBodyOpt = op.WhenFalse;
}
......@@ -167,6 +164,13 @@ private bool ParseIfStatement(IOperation operation, ArrayBuilder<AnalyzedSwitchS
return false;
}
private bool ParseIfStatementOrBlock(IOperation op, ArrayBuilder<AnalyzedSwitchSection> sections, out IOperation? defaultBodyOpt)
{
return op is IBlockOperation block
? ParseIfStatementSequence(block.Operations.AsSpan(), sections, out defaultBodyOpt)
: ParseIfStatement(op, sections, out defaultBodyOpt);
}
private AnalyzedSwitchSection? ParseSwitchSection(IConditionalOperation operation)
{
var labels = ArrayBuilder<AnalyzedSwitchLabel>.GetInstance();
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册