diff --git a/ICSharpCode.Decompiler/Ast/Transforms/PatternStatementTransform.cs b/ICSharpCode.Decompiler/Ast/Transforms/PatternStatementTransform.cs index cd5a7eded..d3bf2f7c1 100644 --- a/ICSharpCode.Decompiler/Ast/Transforms/PatternStatementTransform.cs +++ b/ICSharpCode.Decompiler/Ast/Transforms/PatternStatementTransform.cs @@ -630,8 +630,6 @@ namespace ICSharpCode.Decompiler.Ast.Transforms Match m = switchOnStringPattern.Match(node); if (!m.Success) return null; - if (m.Has("nonNullDefaultStmt") && !m.Has("nullStmt")) - return null; // switchVar must be the same as switchExpr; or switchExpr must be an assignment and switchVar the left side of that assignment if (!m.Get("switchVar").Single().IsMatch(m.Get("switchExpr").Single())) { AssignmentExpression assign = m.Get("switchExpr").Single() as AssignmentExpression; @@ -666,15 +664,21 @@ namespace ICSharpCode.Decompiler.Ast.Transforms block.Statements.Add(new BreakStatement()); section.Statements.Add(block.Detach()); sw.SwitchSections.Add(section); - if (m.Has("nonNullDefaultStmt")) { - section = new SwitchSection(); - section.CaseLabels.Add(new CaseLabel()); - block = new BlockStatement(); - block.Statements.AddRange(m.Get("nonNullDefaultStmt").Select(s => s.Detach())); - block.Add(new BreakStatement()); - section.Statements.Add(block); - sw.SwitchSections.Add(section); - } + } else if (m.Has("nonNullDefaultStmt")) { + sw.SwitchSections.Add( + new SwitchSection { + CaseLabels = { new CaseLabel { Expression = new NullReferenceExpression() } }, + Statements = { new BlockStatement { new BreakStatement() } } + }); + } + if (m.Has("nonNullDefaultStmt")) { + SwitchSection section = new SwitchSection(); + section.CaseLabels.Add(new CaseLabel()); + BlockStatement block = new BlockStatement(); + block.Statements.AddRange(m.Get("nonNullDefaultStmt").Select(s => s.Detach())); + block.Add(new BreakStatement()); + section.Statements.Add(block); + sw.SwitchSections.Add(section); } node.ReplaceWith(sw); return sw; diff --git a/ICSharpCode.Decompiler/Tests/Switch.cs b/ICSharpCode.Decompiler/Tests/Switch.cs index 3f700bbc1..8890a5dab 100644 --- a/ICSharpCode.Decompiler/Tests/Switch.cs +++ b/ICSharpCode.Decompiler/Tests/Switch.cs @@ -17,7 +17,8 @@ public static class Switch public static string SwitchOverString1(string text) { - switch (text) { + switch (text) + { case "First case": return "Text1"; case "Second case": @@ -40,7 +41,8 @@ public static class Switch public static string SwitchOverString2() { - switch (Environment.UserName) { + switch (Environment.UserName) + { case "First case": return "Text1"; case "Second case": diff --git a/ICSharpCode.Decompiler/Tests/TestRunner.cs b/ICSharpCode.Decompiler/Tests/TestRunner.cs index eb6261f7e..998c17370 100644 --- a/ICSharpCode.Decompiler/Tests/TestRunner.cs +++ b/ICSharpCode.Decompiler/Tests/TestRunner.cs @@ -85,7 +85,7 @@ namespace ICSharpCode.Decompiler.Tests TestFile(@"..\..\Tests\QueryExpressions.cs"); } - [Test, Ignore("switch transform is currently broken")] + [Test, Ignore("switch transform doesn't recreate the exact original switch")] public void Switch() { TestFile(@"..\..\Tests\Switch.cs");