Skip to content

Commit

Permalink
[Performance] Improve SpecifyIFormatProvider (CA1305) performance (#6865
Browse files Browse the repository at this point in the history
)
  • Loading branch information
Youssef1313 authored Sep 12, 2023
1 parent fc117a2 commit ff698b7
Showing 1 changed file with 9 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -186,11 +186,11 @@ protected override void InitializeWorker(CompilationStartAnalysisContext context

#region "IFormatProviderAlternateStringRule Only"
if (stringFormatMemberWithIFormatProviderStringAndParamsObjectParameter != null &&
!oaContext.Options.IsConfiguredToSkipAnalysis(IFormatProviderAlternateStringRule, targetMethod, oaContext.ContainingSymbol, oaContext.Compilation) &&
(targetMethod.Equals(stringFormatMemberWithStringAndObjectParameter) ||
targetMethod.Equals(stringFormatMemberWithStringObjectAndObjectParameter) ||
targetMethod.Equals(stringFormatMemberWithStringObjectObjectAndObjectParameter) ||
targetMethod.Equals(stringFormatMemberWithStringAndParamsObjectParameter)))
targetMethod.Equals(stringFormatMemberWithStringAndParamsObjectParameter)) &&
!oaContext.Options.IsConfiguredToSkipAnalysis(IFormatProviderAlternateStringRule, targetMethod, oaContext.ContainingSymbol, oaContext.Compilation))
{
// Sample message for IFormatProviderAlternateStringRule: Because the behavior of string.Format(string, object) could vary based on the current user's locale settings,
// replace this call in IFormatProviderStringTest.M() with a call to string.Format(IFormatProvider, string, params object[]).
Expand Down Expand Up @@ -266,19 +266,16 @@ protected override void InitializeWorker(CompilationStartAnalysisContext context

if (!oaContext.Options.IsConfiguredToSkipAnalysis(uiCultureRule, targetMethod, oaContext.ContainingSymbol, oaContext.Compilation))
{
IEnumerable<int> IformatProviderParameterIndices = GetIndexesOfParameterType(targetMethod, iformatProviderType);
foreach (var index in IformatProviderParameterIndices)
foreach (var argument in invocationExpression.Arguments)
{
var argument = invocationExpression.Arguments[index];

if (argument != null && currentUICultureProperty != null &&
installedUICultureProperty != null && currentThreadCurrentUICultureProperty != null)
if (!iformatProviderType.Equals(argument.Parameter?.Type))
{
var semanticModel = argument.SemanticModel!;

var symbol = semanticModel.GetSymbolInfo(argument.Value.Syntax, oaContext.CancellationToken).Symbol;
continue;
}

if (symbol != null &&
if (currentUICultureProperty != null && installedUICultureProperty != null && currentThreadCurrentUICultureProperty != null)
{
if (argument.Value.WalkDownConversion() is IPropertyReferenceOperation { Property: { } symbol } &&
(symbol.Equals(currentUICultureProperty) ||
symbol.Equals(installedUICultureProperty) ||
symbol.Equals(currentThreadCurrentUICultureProperty) ||
Expand Down Expand Up @@ -324,14 +321,6 @@ static IEnumerable<IMethodSymbol> GetToStringMethods(INamedTypeSymbol namedTypeS
=> namedTypeSymbol.GetMembers("ToString").OfType<IMethodSymbol>().WhereNotNull();
}

private static IEnumerable<int> GetIndexesOfParameterType(IMethodSymbol targetMethod, INamedTypeSymbol formatProviderType)
{
return targetMethod.Parameters
.Select((Parameter, Index) => (Parameter, Index))
.Where(x => x.Parameter.Type.Equals(formatProviderType))
.Select(x => x.Index);
}

private static ParameterInfo GetParameterInfo(INamedTypeSymbol type, bool isArray = false, int arrayRank = 0, bool isParams = false)
{
return ParameterInfo.GetParameterInfo(type, isArray, arrayRank, isParams);
Expand Down

0 comments on commit ff698b7

Please sign in to comment.