Skip to content

Commit f9c7b6b

Browse files
committed
Extend EliminateRedundantTryFinally in ReduceNestingTransform
1 parent 61f79a0 commit f9c7b6b

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

ICSharpCode.Decompiler/IL/Transforms/ReduceNestingTransform.cs

+19-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2018 Siegfried Pammer
1+
// Copyright (c) 2018 Siegfried Pammer
22
//
33
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
44
// software and associated documentation files (the "Software"), to deal in the Software
@@ -637,10 +637,25 @@ leave IL_003e (nop)
637637
// Finally is empty and redundant. But we'll delete the block only if there's a PinnedRegion.
638638
if (!(tryFinally.TryBlock is BlockContainer tryContainer))
639639
return;
640-
if (tryContainer.SingleInstruction() is PinnedRegion pinnedRegion)
640+
if (tryContainer.Blocks.Count != 1)
641+
return;
642+
var tryBlock = tryContainer.Blocks[0];
643+
if (tryBlock.Instructions.Count == 1)
641644
{
642-
context.Step("Removing try-finally around PinnedRegion", pinnedRegion);
643-
tryFinally.ReplaceWith(pinnedRegion);
645+
if (tryBlock.Instructions[0] is PinnedRegion pinnedRegion)
646+
{
647+
context.Step("Removing try-finally around PinnedRegion", pinnedRegion);
648+
tryFinally.ReplaceWith(pinnedRegion);
649+
}
650+
}
651+
else if (tryBlock.Instructions.Count == 2)
652+
{
653+
if (tryBlock.Instructions[0] is PinnedRegion pinnedRegion &&
654+
tryBlock.Instructions[1].MatchLeave(tryContainer))
655+
{
656+
context.Step("Removing try-finally around PinnedRegion", pinnedRegion);
657+
tryFinally.ReplaceWith(pinnedRegion);
658+
}
644659
}
645660
}
646661
}

0 commit comments

Comments
 (0)