Browse Source

Extend `EliminateRedundantTryFinally` in `ReduceNestingTransform`

pull/2959/head
ElektroKill 3 years ago
parent
commit
f9c7b6b66e
No known key found for this signature in database
GPG Key ID: 7E3C5C084E40E3EC
  1. 19
      ICSharpCode.Decompiler/IL/Transforms/ReduceNestingTransform.cs

19
ICSharpCode.Decompiler/IL/Transforms/ReduceNestingTransform.cs

@ -1,4 +1,4 @@
// Copyright (c) 2018 Siegfried Pammer // Copyright (c) 2018 Siegfried Pammer
// //
// Permission is hereby granted, free of charge, to any person obtaining a copy of this // Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software // software and associated documentation files (the "Software"), to deal in the Software
@ -637,11 +637,26 @@ namespace ICSharpCode.Decompiler.IL
// Finally is empty and redundant. But we'll delete the block only if there's a PinnedRegion. // Finally is empty and redundant. But we'll delete the block only if there's a PinnedRegion.
if (!(tryFinally.TryBlock is BlockContainer tryContainer)) if (!(tryFinally.TryBlock is BlockContainer tryContainer))
return; return;
if (tryContainer.SingleInstruction() is PinnedRegion pinnedRegion) if (tryContainer.Blocks.Count != 1)
return;
var tryBlock = tryContainer.Blocks[0];
if (tryBlock.Instructions.Count == 1)
{
if (tryBlock.Instructions[0] is PinnedRegion pinnedRegion)
{ {
context.Step("Removing try-finally around PinnedRegion", pinnedRegion); context.Step("Removing try-finally around PinnedRegion", pinnedRegion);
tryFinally.ReplaceWith(pinnedRegion); tryFinally.ReplaceWith(pinnedRegion);
} }
} }
else if (tryBlock.Instructions.Count == 2)
{
if (tryBlock.Instructions[0] is PinnedRegion pinnedRegion &&
tryBlock.Instructions[1].MatchLeave(tryContainer))
{
context.Step("Removing try-finally around PinnedRegion", pinnedRegion);
tryFinally.ReplaceWith(pinnedRegion);
}
}
}
} }
} }

Loading…
Cancel
Save