From 9e168224ddb970cec2faf674a624fb58e4606c28 Mon Sep 17 00:00:00 2001 From: Siegfried Pammer Date: Sun, 1 Oct 2023 17:58:44 +0200 Subject: [PATCH] Fix #3091: extension methods named "Add" were skipping some checks in AccessPathElement.IsMethodApplicable. --- .../TestCases/Pretty/InitializerTests.cs | 12 ++++++++++++ .../TransformCollectionAndObjectInitializers.cs | 8 ++++++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/InitializerTests.cs b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/InitializerTests.cs index 3844d22cb..9accce88a 100644 --- a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/InitializerTests.cs +++ b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/InitializerTests.cs @@ -34,6 +34,10 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty.InitializerTests public static void Add(this IList> collection, string key, T value, Func convert = null) { } + + public static void Add(this TestCases collection, string key) + { + } } public class TestCases @@ -1104,6 +1108,14 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty.InitializerTests Console.WriteLine(customList); } + public static TestCases NoCollectionInitializerBecauseOfMissingIEnumerable() + { + TestCases testCases = new TestCases(); + testCases.Add("int"); + testCases.Add("string"); + return testCases; + } + public static void CollectionInitializerWithParamsMethod() { X(Y(), new CustomList { { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 } }); diff --git a/ICSharpCode.Decompiler/IL/Transforms/TransformCollectionAndObjectInitializers.cs b/ICSharpCode.Decompiler/IL/Transforms/TransformCollectionAndObjectInitializers.cs index 1f8845d93..781a83c01 100644 --- a/ICSharpCode.Decompiler/IL/Transforms/TransformCollectionAndObjectInitializers.cs +++ b/ICSharpCode.Decompiler/IL/Transforms/TransformCollectionAndObjectInitializers.cs @@ -439,8 +439,12 @@ namespace ICSharpCode.Decompiler.IL.Transforms if (!"Add".Equals(method.Name, StringComparison.Ordinal) || arguments.Count == 0) return false; if (method.IsExtensionMethod) - return settings?.ExtensionMethodsInCollectionInitializers != false - && CSharp.Transforms.IntroduceExtensionMethods.CanTransformToExtensionMethodCall(method, resolveContext, ignoreTypeArguments: true); + { + if (settings?.ExtensionMethodsInCollectionInitializers == false) + return false; + if (!CSharp.Transforms.IntroduceExtensionMethods.CanTransformToExtensionMethodCall(method, resolveContext, ignoreTypeArguments: true)) + return false; + } var targetType = GetReturnTypeFromInstruction(arguments[0]) ?? rootType; if (targetType == null) return false;