Skip to content

Commit

Permalink
optimize IsExpressionTree (#931)
Browse files Browse the repository at this point in the history
  • Loading branch information
omsmith authored Nov 17, 2023
1 parent b01609d commit a96c889
Showing 1 changed file with 14 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ private static void AnalyzeInvocation(

// Don't complain about expression trees, since they aren't allowed
// to have named arguments
if( IsExpressionTree( lambdaExpresssionSymbol, ctx.Operation.SemanticModel!, ctx.Operation ) ) {
if( IsExpressionTree( lambdaExpresssionSymbol, ctx.Operation ) ) {
return;
}

Expand Down Expand Up @@ -172,32 +172,27 @@ private static void AnalyzeInvocation(

private static bool IsExpressionTree(
INamedTypeSymbol? expressionType,
SemanticModel model,
IOperation operation
) {
if( expressionType == null || expressionType.Kind == SymbolKind.ErrorType ) {
return false;
}

// the current call could be nested inside an expression tree, so
// check every call we are nested inside
foreach( var syntax in operation.Syntax.AncestorsAndSelf() ) {
if( !( syntax is InvocationExpressionSyntax || syntax is ObjectCreationExpressionSyntax ) ) {
continue;
}
IOperation? current = operation;
for( ; ; ) {
current = current?.Parent;

if( syntax.Parent is null ) {
if( current is null ) {
break;
}

if( current is not IConversionOperation conversion ) {
continue;
}

var implicitType = model.GetTypeInfo( syntax.Parent ).ConvertedType;
if( implicitType != null && implicitType.Kind != SymbolKind.ErrorType ) {

var baseExprType = implicitType.BaseType;
if( expressionType.OriginalDefinition.Equals( baseExprType, SymbolEqualityComparer.Default ) ) {
return true;
}
}
}

if( SymbolEqualityComparer.Default.Equals( expressionType, conversion.Type?.BaseType ) ) {
return true;
}
}

return false;
Expand Down

0 comments on commit a96c889

Please sign in to comment.