Browse Source

Fix crash in ConvertIfToSwitchAction when there are nested if statements.

pull/38/head
Daniel Grunwald 12 years ago
parent
commit
ed58119f41
  1. 24
      src/Libraries/NRefactory/ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/ConvertIfToSwitchAction.cs

24
src/Libraries/NRefactory/ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/ConvertIfToSwitchAction.cs

@ -174,22 +174,16 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
static void CollectSwitchSectionStatements (AstNodeCollection<Statement> result, RefactoringContext context, static void CollectSwitchSectionStatements (AstNodeCollection<Statement> result, RefactoringContext context,
Statement statement) Statement statement)
{ {
BlockStatement blockStatement; BlockStatement blockStatement = statement as BlockStatement;
if (statement is BlockStatement) if (blockStatement != null)
blockStatement = (BlockStatement)statement.Clone (); result.AddRange(blockStatement.Statements.Select(s => s.Clone()));
else else
blockStatement = new BlockStatement { statement.Clone () }; result.Add(statement.Clone());
var breackStatement = new BreakStatement (); // add 'break;' at end if necessary
blockStatement.Add (breackStatement); var reachabilityAnalysis = context.CreateReachabilityAnalysis (statement);
// check if break is needed if (reachabilityAnalysis.IsEndpointReachable(statement))
var reachabilityAnalysis = context.CreateReachabilityAnalysis (blockStatement); result.Add(new BreakStatement());
if (!reachabilityAnalysis.IsReachable (breackStatement))
blockStatement.Statements.Remove (breackStatement);
var statements = blockStatement.Statements.ToArray ();
blockStatement.Statements.Clear ();
result.AddRange (statements);
} }
} }
} }

Loading…
Cancel
Save