Skip to content

Commit

Permalink
[Performance] Improve UseValidPlatformString (CA1418) performance (#6867
Browse files Browse the repository at this point in the history
)
  • Loading branch information
Youssef1313 authored Sep 12, 2023
1 parent ff698b7 commit 837c22a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,8 @@ static bool NameAndParametersValid(IMethodSymbol method) =>

private static void AnalyzeOperation(IOperation operation, OperationAnalysisContext context, PooledDictionary<string, int> knownPlatforms)
{
if (operation is IInvocationOperation invocation &&
methodNames.Contains(invocation.TargetMethod.Name) &&
var invocation = (IInvocationOperation)operation;
if (methodNames.Contains(invocation.TargetMethod.Name) &&
invocation.Arguments.Length > 0 &&
invocation.Arguments[0].Value is { } argument &&
argument.ConstantValue.HasValue &&
Expand Down
25 changes: 25 additions & 0 deletions src/Utilities/Compiler/Extensions/ISymbolExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,31 @@ public static IEnumerable<AttributeData> GetAttributes(this ISymbol symbol, para
return symbol.GetAttributes(attributesToMatch: attributeTypesToMatch);
}

#region "Overloads to avoid array allocations"
public static IEnumerable<AttributeData> GetAttributes(this ISymbol symbol, INamedTypeSymbol? attributeTypeToMatch1)
{
foreach (var attribute in symbol.GetAttributes())
{
if (SymbolEqualityComparer.Default.Equals(attribute.AttributeClass, attributeTypeToMatch1))
{
yield return attribute;
}
}
}

public static IEnumerable<AttributeData> GetAttributes(this ISymbol symbol, INamedTypeSymbol? attributeTypeToMatch1, INamedTypeSymbol? attributeTypeToMatch2)
{
foreach (var attribute in symbol.GetAttributes())
{
if (SymbolEqualityComparer.Default.Equals(attribute.AttributeClass, attributeTypeToMatch1) ||
SymbolEqualityComparer.Default.Equals(attribute.AttributeClass, attributeTypeToMatch2))
{
yield return attribute;
}
}
}
#endregion

public static bool HasAnyAttribute(this ISymbol symbol, IEnumerable<INamedTypeSymbol> attributesToMatch)
{
return symbol.GetAttributes(attributesToMatch).Any();
Expand Down

0 comments on commit 837c22a

Please sign in to comment.