Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Abort SwitchAnalysis on duplicate condition (redundant code) #1295

Merged
merged 2 commits into from
Nov 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions ICSharpCode.Decompiler.Tests/TestCases/Correctness/Loops.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ static void Main()
Console.WriteLine(NoForeachDueToMultipleCurrentAccess(new List<int> { 1, 2, 3, 4, 5 }));
Console.WriteLine(NoForeachCallWithSideEffect(new CustomClassEnumeratorWithIDisposable<int>()));
LoopWithGotoRepeat();
Console.WriteLine("LoopFollowedByIf: {0}", LoopFollowedByIf());
}

public static void ForWithMultipleVariables()
Expand Down Expand Up @@ -246,5 +247,17 @@ static void LoopWithGotoRepeat()
}
Console.WriteLine("after finally");
}

private static int LoopFollowedByIf()
{
int num = 0;
while (num == 0) {
num++;
}
if (num == 0) {
return -1;
}
return num;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ internal class Issue959
{
public void Test(bool arg)
{
switch (arg) {
if (!arg && arg) {

}
}
Expand Down
2 changes: 2 additions & 0 deletions ICSharpCode.Decompiler/IL/ControlFlow/SwitchAnalysis.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ bool AnalyzeBlock(Block block, LongSet inputValues, bool tailOnly = false)
if (!(tailOnly || block.Instructions.Count == 2))
return false;
trueValues = trueValues.IntersectWith(inputValues);
if (trueValues.SetEquals(inputValues) || trueValues.IsEmpty)
return false;
Block trueBlock;
if (trueInst.MatchBranch(out trueBlock) && AnalyzeBlock(trueBlock, trueValues)) {
// OK, true block was further analyzed.
Expand Down