Browse Source

[CodeAction]ConvertIfToSwitchAction: avoid generating redundant default section.

newNRvisualizers
Mansheng Yang 13 years ago
parent
commit
c93ef39474
  1. 3
      ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/ConvertIfToSwitchAction.cs
  2. 33
      ICSharpCode.NRefactory.Tests/CSharp/CodeActions/ConvertIfToSwtichTests.cs

3
ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/ConvertIfToSwitchAction.cs

@ -121,6 +121,9 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -121,6 +121,9 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
CollectSwitchSectionStatements (section.Statements, context, ifStatement.TrueStatement);
result.Add (section);
if (ifStatement.FalseStatement.IsNull)
return true;
// else if
var falseStatement = ifStatement.FalseStatement as IfElseStatement;
if (falseStatement != null)

33
ICSharpCode.NRefactory.Tests/CSharp/CodeActions/ConvertIfToSwtichTests.cs

@ -433,6 +433,39 @@ class TestClass @@ -433,6 +433,39 @@ class TestClass
}
}");
}
[Test]
public void TestNoElse()
{
Test<ConvertIfToSwitchAction> (@"
class TestClass
{
void TestMethod (int a)
{
int b;
if$ (a == 0) {
b = 0;
} else if (a == 1) {
b = 1;
}
}
}", @"
class TestClass
{
void TestMethod (int a)
{
int b;
switch (a) {
case 0:
b = 0;
break;
case 1:
b = 1;
break;
}
}
}");
}
}
}

Loading…
Cancel
Save