diff --git a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/Switch.cs b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/Switch.cs index 6878f19a9..eb196e240 100644 --- a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/Switch.cs +++ b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/Switch.cs @@ -1193,6 +1193,26 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty } } + public static void Issue1745(string aaa) + { + switch (aaa) { + case "a": + case "b": + case "c": + case "d": + case "e": + case "f": + Console.WriteLine(aaa); + break; + case null: + Console.WriteLine(""); + break; + case "": + Console.WriteLine(""); + break; + } + } + public static bool DoNotRemoveAssignmentBeforeSwitch(string x, out ConsoleKey key) { key = (ConsoleKey)0; diff --git a/ICSharpCode.Decompiler/IL/Instructions/StringToInt.cs b/ICSharpCode.Decompiler/IL/Instructions/StringToInt.cs index f000eb36d..881ca2c03 100644 --- a/ICSharpCode.Decompiler/IL/Instructions/StringToInt.cs +++ b/ICSharpCode.Decompiler/IL/Instructions/StringToInt.cs @@ -55,7 +55,10 @@ namespace ICSharpCode.Decompiler.IL int i = 0; foreach (var entry in Map) { if (i > 0) output.Write(", "); - output.Write($"[\"{entry.Key}\"] = {entry.Value}"); + if (entry.Key is null) + output.Write($"[null] = {entry.Value}"); + else + output.Write($"[\"{entry.Key}\"] = {entry.Value}"); i++; } output.Write(" })"); diff --git a/ICSharpCode.Decompiler/IL/Transforms/SwitchOnStringTransform.cs b/ICSharpCode.Decompiler/IL/Transforms/SwitchOnStringTransform.cs index c9c1cd804..8c62262ac 100644 --- a/ICSharpCode.Decompiler/IL/Transforms/SwitchOnStringTransform.cs +++ b/ICSharpCode.Decompiler/IL/Transforms/SwitchOnStringTransform.cs @@ -449,7 +449,7 @@ namespace ICSharpCode.Decompiler.IL.Transforms return false; if (!tryGetValueBlock.Instructions[0].MatchIfInstruction(out condition, out var defaultBlockJump)) return false; - if (!defaultBlockJump.MatchBranch(out var defaultBlock) && !defaultBlockJump.MatchLeave(leaveContainer)) + if (!defaultBlockJump.MatchBranch(out var defaultBlock) && !((leaveContainer != null && defaultBlockJump.MatchLeave(leaveContainer)) || defaultBlockJump.MatchLeave(out _))) return false; if (!(condition.MatchLogicNot(out var arg) && arg is CallInstruction c && c.Method.Name == "TryGetValue" && MatchDictionaryFieldLoad(c.Arguments[0], IsStringToIntDictionary, out var dictField2, out _) && dictField2.Equals(dictField))) @@ -557,9 +557,9 @@ namespace ICSharpCode.Decompiler.IL.Transforms return true; } - bool AddNullSection(List sections, List<(string, int)> stringValues, Block nullValueCaseBlock) + bool AddNullSection(List sections, List<(string Value, int Index)> stringValues, Block nullValueCaseBlock) { - var label = new LongSet(sections.Count); + var label = new LongSet(stringValues.Max(item => item.Index) + 1); var possibleConflicts = sections.Where(sec => sec.Labels.Overlaps(label)).ToArray(); if (possibleConflicts.Length > 1) return false; @@ -984,7 +984,7 @@ namespace ICSharpCode.Decompiler.IL.Transforms return false; if (!MatchStringLengthCall(lengthCheckCondition, switchValueVar)) return false; - if (!(exitBranch.MatchBranch(out defaultOrExitBlock) && exitBranch2.MatchBranch(defaultOrExitBlock))) + if (!exitBranch.Match(exitBranch2).Success) return false; if (bodyBranch.MatchLeave(out _)) { bodyOrLeave = bodyBranch;