Skip to content

Commit 99c10a6

Browse files
dgrunwaldElektroKill
authored andcommitted
Merge pull request icsharpcode#1295 from Chicken-Bones/switchdetection
Abort SwitchAnalysis on duplicate condition (redundant code)
1 parent fb573dc commit 99c10a6

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

ICSharpCode.Decompiler.Tests/TestCases/Correctness/Loops.cs

+13
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ static void Main()
7979
Console.WriteLine(NoForeachDueToMultipleCurrentAccess(new List<int> { 1, 2, 3, 4, 5 }));
8080
Console.WriteLine(NoForeachCallWithSideEffect(new CustomClassEnumeratorWithIDisposable<int>()));
8181
LoopWithGotoRepeat();
82+
Console.WriteLine("LoopFollowedByIf: {0}", LoopFollowedByIf());
8283
}
8384

8485
public static void ForWithMultipleVariables()
@@ -246,5 +247,17 @@ static void LoopWithGotoRepeat()
246247
}
247248
Console.WriteLine("after finally");
248249
}
250+
251+
private static int LoopFollowedByIf()
252+
{
253+
int num = 0;
254+
while (num == 0) {
255+
num++;
256+
}
257+
if (num == 0) {
258+
return -1;
259+
}
260+
return num;
261+
}
249262
}
250263
}

ICSharpCode.Decompiler.Tests/TestCases/ILPretty/Issue959.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ internal class Issue959
44
{
55
public void Test(bool arg)
66
{
7-
switch (arg) {
7+
if (!arg && arg) {
88

99
}
1010
}

ICSharpCode.Decompiler/IL/ControlFlow/SwitchAnalysis.cs

+2
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ bool AnalyzeBlock(Block block, LongSet inputValues, bool tailOnly = false)
116116
if (!(tailOnly || block.Instructions.Count == 2))
117117
return false;
118118
trueValues = trueValues.IntersectWith(inputValues);
119+
if (trueValues.SetEquals(inputValues) || trueValues.IsEmpty)
120+
return false;
119121
Block trueBlock;
120122
if (trueInst.MatchBranch(out trueBlock) && AnalyzeBlock(trueBlock, trueValues)) {
121123
// OK, true block was further analyzed.

0 commit comments

Comments
 (0)