提交 c8840765 编写于 作者: C Cyrus Najmabadi

Do not offer to use ?. when in an query that will become an expression tree.

上级 423c52f2
......@@ -412,6 +412,65 @@ public void Method<T>(Expression<Func<T, string>> functor)
}");
}
[WorkItem(33992, "https://github.com/dotnet/roslyn/issues/33992")]
[WorkItem(17623, "https://github.com/dotnet/roslyn/issues/17623")]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseNullPropagation)]
public async Task TestInExpressionTree2()
{
await TestMissingInRegularAndScriptAsync(
@"
using System.Linq;
class C
{
void Main()
{
_ = from item in Enumerable.Empty<(int? x, int? y)?>().AsQueryable()
select [||]item == null ? null : item.Value.x;
}
}");
}
[WorkItem(33992, "https://github.com/dotnet/roslyn/issues/33992")]
[WorkItem(17623, "https://github.com/dotnet/roslyn/issues/17623")]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseNullPropagation)]
public async Task TestInExpressionTree3()
{
await TestMissingInRegularAndScriptAsync(
@"
using System.Linq;
class C
{
void Main()
{
_ = from item in Enumerable.Empty<(int? x, int? y)?>().AsQueryable()
where ([||]item == null ? null : item.Value.x) > 0
select item;
}
}");
}
[WorkItem(33992, "https://github.com/dotnet/roslyn/issues/33992")]
[WorkItem(17623, "https://github.com/dotnet/roslyn/issues/17623")]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseNullPropagation)]
public async Task TestInExpressionTree4()
{
await TestMissingInRegularAndScriptAsync(
@"
using System.Linq;
class C
{
void Main()
{
_ = from item in Enumerable.Empty<(int? x, int? y)?>().AsQueryable()
let x = [||]item == null ? null : item.Value.x
select x;
}
}");
}
[WorkItem(19774, "https://github.com/dotnet/roslyn/issues/19774")]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseNullPropagation)]
public async Task TestNullableMemberAccess()
......
......@@ -715,5 +715,57 @@ Class C
End Class")
End Function
<WorkItem(33992, "https://github.com/dotnet/roslyn/issues/33992")>
<Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseNullPropagation)>
Public Async Function TestExpressionTree1() As Task
Await TestMissingInRegularAndScriptAsync(
"
Imports System
Imports System.Linq
Public Class Class1
Public Sub Foo()
Dim q = From item In Enumerable.Empty(Of (x As Integer?, y As Integer?)?)().AsQueryable()
Select [||]If(item Is Nothing, Nothing, item.Value.x)
End Sub
End Class")
End Function
<WorkItem(33992, "https://github.com/dotnet/roslyn/issues/33992")>
<Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseNullPropagation)>
Public Async Function TestExpressionTree2() As Task
Await TestMissingInRegularAndScriptAsync(
"
Imports System
Imports System.Linq
Public Class Class1
Public Sub Foo()
Dim q = From item In Enumerable.Empty(Of (x As Integer?, y As Integer?)?)().AsQueryable()
Where [||]If(item Is Nothing, Nothing, item.Value.x) > 0
Select item
End Sub
End Class")
End Function
<WorkItem(33992, "https://github.com/dotnet/roslyn/issues/33992")>
<Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseNullPropagation)>
Public Async Function TestExpressionTree3() As Task
Await TestMissingInRegularAndScriptAsync(
"
Imports System
Imports System.Linq
Public Class Class1
Public Sub Foo()
Dim q = From item In Enumerable.Empty(Of (x As Integer?, y As Integer?)?)().AsQueryable()
Let x = [||]If(item Is Nothing, Nothing, item.Value.x)
Select x
End Sub
End Class")
End Function
End Class
End Namespace
......@@ -11,6 +11,7 @@
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Threading;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.LanguageServices;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Shared.Extensions;
......@@ -906,7 +907,7 @@ public static bool ContainsInArgument(this ConstructorInitializerSyntax initiali
return false;
}
return initializer.ArgumentList.Arguments.Any(a => a.Span.Contains(textSpan));
return initializer.ArgumentList.Arguments.Any<ArgumentSyntax>(a => a.Span.Contains(textSpan));
}
public static bool ContainsInBlockBody(this BlockSyntax block, TextSpan textSpan)
......@@ -962,6 +963,25 @@ public static IEnumerable<MemberDeclarationSyntax> GetMembers(this SyntaxNode no
{
var typeInfo = semanticModel.GetTypeInfo(current, cancellationToken);
if (expressionTypeOpt.Equals(typeInfo.ConvertedType?.OriginalDefinition))
return true;
}
else if (current is SelectOrGroupClauseSyntax clause)
{
var info = semanticModel.GetSymbolInfo(clause, cancellationToken);
if (TakesExpressionTree(info, expressionTypeOpt))
return true;
}
else if (current is OrderingSyntax ordering)
{
var info = semanticModel.GetSymbolInfo(ordering, cancellationToken);
if (TakesExpressionTree(info, expressionTypeOpt))
return true;
}
else if (current is QueryClauseSyntax queryClause)
{
var info = semanticModel.GetQueryClauseInfo(queryClause, cancellationToken);
if (TakesExpressionTree(info.CastInfo, expressionTypeOpt) ||
TakesExpressionTree(info.OperationInfo, expressionTypeOpt))
{
return true;
}
......@@ -970,6 +990,21 @@ public static IEnumerable<MemberDeclarationSyntax> GetMembers(this SyntaxNode no
}
return false;
static bool TakesExpressionTree(SymbolInfo info, INamedTypeSymbol expressionType)
{
foreach (var symbol in info.GetAllSymbols())
{
if (symbol is IMethodSymbol method &&
method.Parameters.Length > 0 &&
expressionType.Equals(method.Parameters[0].Type?.OriginalDefinition))
{
return true;
}
}
return false;
}
}
public static bool IsInDeconstructionLeft(
......
......@@ -1052,10 +1052,26 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Extensions
While current IsNot Nothing
If SyntaxFacts.IsSingleLineLambdaExpression(current.Kind) OrElse
SyntaxFacts.IsMultiLineLambdaExpression(current.Kind) Then
Dim TypeInfo = semanticModel.GetTypeInfo(current, cancellationToken)
If expressionTypeOpt.Equals(TypeInfo.ConvertedType?.OriginalDefinition) Then
Dim typeInfo = semanticModel.GetTypeInfo(current, cancellationToken)
If expressionTypeOpt.Equals(typeInfo.ConvertedType?.OriginalDefinition) Then
Return True
End If
ElseIf TypeOf current Is OrderingSyntax OrElse
TypeOf current Is QueryClauseSyntax OrElse
TypeOf current Is FunctionAggregationSyntax OrElse
TypeOf current Is ExpressionRangeVariableSyntax Then
Dim info = semanticModel.GetSymbolInfo(current, cancellationToken)
For Each symbol In info.GetAllSymbols()
Dim method = TryCast(symbol, IMethodSymbol)
If method IsNot Nothing AndAlso
method.Parameters.Length > 0 AndAlso
expressionTypeOpt.Equals(method.Parameters(0).Type.OriginalDefinition) Then
Return True
End If
Next
End If
current = current.Parent
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册