Skip to content

Commit 9e16822

Browse files
Fix #3091: extension methods named "Add" were skipping some checks in AccessPathElement.IsMethodApplicable.
1 parent d2d1c33 commit 9e16822

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ public static void Add(this TestCases.CustomList<int> inst, string a, string b)
3434
public static void Add<T>(this IList<KeyValuePair<string, string>> collection, string key, T value, Func<T, string> convert = null)
3535
{
3636
}
37+
38+
public static void Add(this TestCases collection, string key)
39+
{
40+
}
3741
}
3842

3943
public class TestCases
@@ -1104,6 +1108,14 @@ public static void NoCollectionInitializerBecauseOfTypeArguments()
11041108
Console.WriteLine(customList);
11051109
}
11061110

1111+
public static TestCases NoCollectionInitializerBecauseOfMissingIEnumerable()
1112+
{
1113+
TestCases testCases = new TestCases();
1114+
testCases.Add("int");
1115+
testCases.Add("string");
1116+
return testCases;
1117+
}
1118+
11071119
public static void CollectionInitializerWithParamsMethod()
11081120
{
11091121
X(Y(), new CustomList<int> { { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 } });

ICSharpCode.Decompiler/IL/Transforms/TransformCollectionAndObjectInitializers.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -439,8 +439,12 @@ static bool IsMethodApplicable(IMethod method, IReadOnlyList<ILInstruction> argu
439439
if (!"Add".Equals(method.Name, StringComparison.Ordinal) || arguments.Count == 0)
440440
return false;
441441
if (method.IsExtensionMethod)
442-
return settings?.ExtensionMethodsInCollectionInitializers != false
443-
&& CSharp.Transforms.IntroduceExtensionMethods.CanTransformToExtensionMethodCall(method, resolveContext, ignoreTypeArguments: true);
442+
{
443+
if (settings?.ExtensionMethodsInCollectionInitializers == false)
444+
return false;
445+
if (!CSharp.Transforms.IntroduceExtensionMethods.CanTransformToExtensionMethodCall(method, resolveContext, ignoreTypeArguments: true))
446+
return false;
447+
}
444448
var targetType = GetReturnTypeFromInstruction(arguments[0]) ?? rootType;
445449
if (targetType == null)
446450
return false;

0 commit comments

Comments
 (0)