From ed58119f41351310d5302cea2087f0a2168c08c9 Mon Sep 17 00:00:00 2001 From: Daniel Grunwald Date: Sat, 1 Jun 2013 19:37:28 +0200 Subject: [PATCH] Fix crash in ConvertIfToSwitchAction when there are nested if statements. --- .../CodeActions/ConvertIfToSwitchAction.cs | 24 +++++++------------ 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/src/Libraries/NRefactory/ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/ConvertIfToSwitchAction.cs b/src/Libraries/NRefactory/ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/ConvertIfToSwitchAction.cs index a7ac1bb7c4..a7410fcd02 100644 --- a/src/Libraries/NRefactory/ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/ConvertIfToSwitchAction.cs +++ b/src/Libraries/NRefactory/ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/ConvertIfToSwitchAction.cs @@ -174,22 +174,16 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring static void CollectSwitchSectionStatements (AstNodeCollection result, RefactoringContext context, Statement statement) { - BlockStatement blockStatement; - if (statement is BlockStatement) - blockStatement = (BlockStatement)statement.Clone (); + BlockStatement blockStatement = statement as BlockStatement; + if (blockStatement != null) + result.AddRange(blockStatement.Statements.Select(s => s.Clone())); else - blockStatement = new BlockStatement { statement.Clone () }; - - var breackStatement = new BreakStatement (); - blockStatement.Add (breackStatement); - // check if break is needed - var reachabilityAnalysis = context.CreateReachabilityAnalysis (blockStatement); - if (!reachabilityAnalysis.IsReachable (breackStatement)) - blockStatement.Statements.Remove (breackStatement); - - var statements = blockStatement.Statements.ToArray (); - blockStatement.Statements.Clear (); - result.AddRange (statements); + result.Add(statement.Clone()); + + // add 'break;' at end if necessary + var reachabilityAnalysis = context.CreateReachabilityAnalysis (statement); + if (reachabilityAnalysis.IsEndpointReachable(statement)) + result.Add(new BreakStatement()); } } }