Browse Source

Fix #3091: extension methods named "Add" were skipping some checks in AccessPathElement.IsMethodApplicable.

pull/3106/head
Siegfried Pammer 2 years ago
parent
commit
9e168224dd
  1. 12
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/InitializerTests.cs
  2. 8
      ICSharpCode.Decompiler/IL/Transforms/TransformCollectionAndObjectInitializers.cs

12
ICSharpCode.Decompiler.Tests/TestCases/Pretty/InitializerTests.cs

@ -34,6 +34,10 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty.InitializerTests
public static void Add<T>(this IList<KeyValuePair<string, string>> collection, string key, T value, Func<T, string> convert = null) public static void Add<T>(this IList<KeyValuePair<string, string>> collection, string key, T value, Func<T, string> convert = null)
{ {
} }
public static void Add(this TestCases collection, string key)
{
}
} }
public class TestCases public class TestCases
@ -1104,6 +1108,14 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty.InitializerTests
Console.WriteLine(customList); Console.WriteLine(customList);
} }
public static TestCases NoCollectionInitializerBecauseOfMissingIEnumerable()
{
TestCases testCases = new TestCases();
testCases.Add("int");
testCases.Add("string");
return testCases;
}
public static void CollectionInitializerWithParamsMethod() public static void CollectionInitializerWithParamsMethod()
{ {
X(Y(), new CustomList<int> { { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 } }); X(Y(), new CustomList<int> { { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 } });

8
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) if (!"Add".Equals(method.Name, StringComparison.Ordinal) || arguments.Count == 0)
return false; return false;
if (method.IsExtensionMethod) 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; var targetType = GetReturnTypeFromInstruction(arguments[0]) ?? rootType;
if (targetType == null) if (targetType == null)
return false; return false;

Loading…
Cancel
Save