Skip to content

Commit

Permalink
Don't bother trying to deal with nullables within expressions - closes
Browse files Browse the repository at this point in the history
  • Loading branch information
GrahamTheCoder committed Jun 25, 2023
1 parent 71b3b81 commit f3ab0bf
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ public ExpressionSyntax InvokeConversionWhenNotNull(VBSyntax.ExpressionSyntax vb

public ExpressionSyntax WithBinaryExpressionLogicForNullableTypes(VBSyntax.BinaryExpressionSyntax vbNode, TypeInfo lhsTypeInfo, TypeInfo rhsTypeInfo, BinaryExpressionSyntax csBinExp, ExpressionSyntax lhs, ExpressionSyntax rhs)
{
if (!IsSupported(vbNode.Kind()) ||
if (IsWithinQuery ||
!IsSupported(vbNode.Kind()) ||
!lhsTypeInfo.ConvertedType.IsNullable() ||
!rhsTypeInfo.ConvertedType.IsNullable()) {
return csBinExp;
Expand Down Expand Up @@ -255,6 +256,10 @@ private ExpressionSyntax HasValue(bool reusable, ref ExpressionSyntax csName)

private bool IsSafelyReusable(VBasic.Syntax.ExpressionSyntax e)
{
// In general, you can't rely on a query to safely reuse properties (e.g. it may be compiled to SQL and use it 0 times)
// Since the code to guarantee the same reuse would be very complex and often not useful (e.g. pulling out methods to compose expressions), we just don't guarantee it
// This is a trade off, see https://github.com/icsharpcode/CodeConverter/blob/master/.github/CONTRIBUTING.md#deciding-what-the-output-should-be
if (IsWithinQuery) return true;
e = e.SkipIntoParens();
if (e is VBSyntax.LiteralExpressionSyntax) return true;
var symbolInfo = VBasic.VisualBasicExtensions.GetSymbolInfo(_semanticModel, e);
Expand Down
43 changes: 43 additions & 0 deletions Tests/CSharp/ExpressionTests/LinqExpressionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -822,6 +822,49 @@ from x in list2
}


[Fact]
public async Task Issue1011_LinqExpressionWithNullableCharacterizationAsync()
{
await TestConversionVisualBasicToCSharpAsync(@"Public Class ConversionTest2
Private Class MyEntity
Property FavoriteNumber As Integer?
Property Name As String
End Class
Private Sub BugRepro()
Dim entities As New List(Of MyEntity)
Dim result As String = (From e In entities
Where e.FavoriteNumber = 123
Select e.Name).Single
End Sub
End Class
",
@"using System.Collections.Generic;
using System.Linq;
public partial class ConversionTest2
{
private partial class MyEntity
{
public int? FavoriteNumber { get; set; }
public string Name { get; set; }
}
private void BugRepro()
{
var entities = new List<MyEntity>();
string result = (from e in entities
where e.FavoriteNumber.Value == 123
select e.Name).Single();
}
}");
}


[Fact]
public async Task AnExpressionTreeMayNotContainIsAsync()
{
Expand Down
2 changes: 1 addition & 1 deletion Tests/CSharp/TypeCastTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1566,7 +1566,7 @@ private static void LinqWithNullable()
{
var a = new List<int?>() { 1, 2, 3, default };
var result = from x in a
where x is { } arg1 && arg1 == 1
where x == 1
select x;
}
}");
Expand Down

0 comments on commit f3ab0bf

Please sign in to comment.