From ce199fad134495f4f185786990f490a901f3259a Mon Sep 17 00:00:00 2001 From: Siegfried Pammer Date: Wed, 26 May 2021 23:55:20 +0200 Subject: [PATCH] Fixed regression around foreach-on-string introduced in 16134e52e47be70461aee0ecd5c01c2dd3c8487c --- .../TestCases/Pretty/Loops.cs | 13 +++++++++++++ .../CSharp/Transforms/PatternStatementTransform.cs | 3 +-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/Loops.cs b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/Loops.cs index 07019bc10..28fe3d0c0 100644 --- a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/Loops.cs +++ b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/Loops.cs @@ -20,6 +20,7 @@ using System; using System.Collections; using System.Collections.Generic; using System.Runtime.InteropServices; +using System.Text; namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty { @@ -614,6 +615,18 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty } } + private static void AppendNamePart(string part, StringBuilder name) + { + foreach (char c in part) + { + if (c == '\\') + { + name.Append('\\'); + } + name.Append(c); + } + } + public void NoForeachOverArray(string[] array) { for (int i = 0; i < array.Length; i++) diff --git a/ICSharpCode.Decompiler/CSharp/Transforms/PatternStatementTransform.cs b/ICSharpCode.Decompiler/CSharp/Transforms/PatternStatementTransform.cs index 718c983b2..02978e5e4 100644 --- a/ICSharpCode.Decompiler/CSharp/Transforms/PatternStatementTransform.cs +++ b/ICSharpCode.Decompiler/CSharp/Transforms/PatternStatementTransform.cs @@ -348,10 +348,9 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms var itemVariable = m.Get("itemVariable").Single().GetILVariable(); var indexVariable = m.Get("indexVariable").Single().GetILVariable(); var arrayVariable = m.Get("arrayVariable").Single().GetILVariable(); - var loopContainer = forStatement.Annotation(); if (itemVariable == null || indexVariable == null || arrayVariable == null) return null; - if (arrayVariable.Type.Kind != TypeKind.Array) + if (arrayVariable.Type.Kind != TypeKind.Array && !arrayVariable.Type.IsKnownType(KnownTypeCode.String)) return null; if (!VariableCanBeUsedAsForeachLocal(itemVariable, forStatement)) return null;