Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract checking for generic constraint context to extension method #61485

Merged
merged 1 commit into from
May 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ private static CSharpSyntaxContext CreateContextWorker(Document document, Semant
isEnumTypeMemberAccessContext: syntaxTree.IsEnumTypeMemberAccessContext(semanticModel, position, cancellationToken),
isFixedVariableDeclarationContext: syntaxTree.IsFixedVariableDeclarationContext(position, leftToken),
isFunctionPointerTypeArgumentContext: syntaxTree.IsFunctionPointerTypeArgumentContext(position, leftToken, cancellationToken),
isGenericConstraintContext: targetToken.Parent.IsKind(SyntaxKind.TypeParameterConstraintClause) && targetToken.IsKind(SyntaxKind.ColonToken, SyntaxKind.CommaToken),
isGenericConstraintContext: syntaxTree.IsGenericConstraintContext(targetToken),
isGenericTypeArgumentContext: syntaxTree.IsGenericTypeArgumentContext(position, leftToken, cancellationToken),
isGlobalStatementContext: isGlobalStatementContext,
isImplicitOrExplicitOperatorTypeContext: syntaxTree.IsImplicitOrExplicitOperatorTypeContext(position, leftToken),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -947,6 +947,9 @@ public static bool IsFunctionPointerTypeArgumentContext(
};
}

public static bool IsGenericConstraintContext(this SyntaxTree syntaxTree, SyntaxToken targetToken)
=> targetToken.Parent.IsKind(SyntaxKind.TypeParameterConstraintClause) && targetToken.IsKind(SyntaxKind.ColonToken, SyntaxKind.CommaToken);

public static bool IsGenericTypeArgumentContext(
this SyntaxTree syntaxTree,
int position,
Expand Down Expand Up @@ -1011,12 +1014,12 @@ public static bool IsGenericTypeArgumentContext(
var symbols = semanticModelOpt.LookupName(nameToken, cancellationToken);
return symbols.Any(s =>
{
switch (s)
return s switch
{
case INamedTypeSymbol nt: return nt.Arity > 0;
case IMethodSymbol m: return m.Arity > 0;
default: return false;
}
INamedTypeSymbol nt => nt.Arity > 0,
IMethodSymbol m => m.Arity > 0,
_ => false,
};
});
}

Expand Down