icsharpcode / CodeConverter

Convert code from C# to VB.NET and vice versa using Roslyn
https://icsharpcode.github.io/CodeConverter/
MIT License
840 stars 218 forks source link

VB -> C#: IEnumerable linq extension methods are replaced with static Enumerable methods #1149

Open gaschd opened 1 week ago

gaschd commented 1 week ago

IEnumerable.Select is replaced with Enumerable.Select if predicate is empty. Same happens to .Zip() and .OrderBy() but not to .Where().

It seems to be depended on the extension method used afterwards, while .Sum() and .Max() cause this bug, .Count() does not.

Tested with different numeric types, resulting in the same issue.

Also one generic type parameter is missing from the methods so it does not compile, e.g. .Select needs two but only has one or .Zip needs three but only has two.

VB.Net input code

    Public Class TestClass
        Public Shared Sub Test()
            Dim intCol = New Integer() {1, 2, 3}
            Dim intCol2 = New Integer() {1, 2, 3}

            Dim intColQuery = intCol.Select(Function(x) x)
            Dim intColCopy = intCol.Select(Function(x) x).ToArray()
            Dim intSum = intCol.Select(Function(x) x).Sum(Function(x) x)
            Dim intMax = intCol.Select(Function(x) x).Max(Function(x) x)
            Dim intCnt = intCol.Select(Function(x) x).Count(Function(x) x > 1)

            Dim intSum2 = intCol.Select(Function(x) x).Sum()
            Dim intMax2 = intCol.OrderBy(Function(x) x).Max()
            Dim intCnt2 = intCol.Select(Function(x) x).Count()
            Dim intSum3 = intCol.Zip(intCol2, Function(x, y) x + y).Sum()
        End Sub
    End Class

Erroneous output

public class TestClass
{
    public static void Test()
    {
        int[] intCol = new int[] { 1, 2, 3 };
        int[] intCol2 = new int[] { 1, 2, 3 };

        var intColQuery = intCol.Select(x => x);
        int[] intColCopy = intCol.Select(x => x).ToArray();
        int intSum = intCol.Select(x => x).Sum(x => x);
        int intMax = intCol.Select(x => x).Max(x => x);
        int intCnt = intCol.Select(x => x).Count(x => x > 1);

        var intSum2 = Enumerable.Select<int>((IEnumerable<int>)intCol, x => x).Sum();
        var intMax2 = Enumerable.OrderBy<int>((IEnumerable<int>)intCol, x => x).Max();
        int intCnt2 = intCol.Select(x => x).Count();
        var intSum3 = Enumerable.Zip<int, int>((IEnumerable<int>)intCol, (IEnumerable<int>)intCol2, (x, y) => x + y).Sum();
    }
}

Expected output

    public class TestClass
    {
        public static void Test()
        {
            int[] intCol = new int[] { 1, 2, 3 };
            int[] intCol2 = new int[] { 1, 2, 3 };

            var intColQuery = intCol.Select(x => x);
            int[] intColCopy = intCol.Select(x => x).ToArray();
            int intSum = intCol.Select(x => x).Sum(x => x);
            int intMax = intCol.Select(x => x).Max(x => x);
            int intCnt = intCol.Select(x => x).Count(x => x > 1);

            int intSum2 = intCol.Select(x => x).Sum();
            int intMax2 = intCol.OrderBy(x => x).Max();
            int intCnt2 = intCol.Select(x => x).Count();
            int intSum3 = intCol.Zip(intCol2, (x, y) => x + y).Sum();
        }
    }

Details

gaschd commented 1 week ago

The IsOriginalSymbolGenericMethod func in VbNameExpander returns true for the correct case (IsReducedTypeParameterMethod returns true), but is false for the wrongly converted case. Overwriting it with true yields the expected output for the input code.

The intSum assignment Method Function System.Collections.Generic.IEnumerable(Of System.Int32).Sum(selector As System.Func(Of System.Int32, System.Int32)) As System.Int32 is reduced to the generic form:

Method Function System.Linq.Enumerable.Sum(Of TSource)(source As System.Collections.Generic.IEnumerable(Of TSource), selector As System.Func(Of TSource, System.Int32)) As System.Int32

while the intSum2 assignment Method Function System.Collections.Generic.IEnumerable(Of System.Int32).Sum() As System.Int32 is reduced to a non generic form: Method Function System.Linq.Enumerable.Sum(source As System.Collections.Generic.IEnumerable(Of System.Int32)) As System.Int32

GrahamTheCoder commented 1 week ago

Thanks for the report. Sounds like you're close to a fix, feel free to PR ☺️