Skip to content

Commit 96dadc3

Browse files
committed
[Performance] Improve SpecifyIFormatProvider (CA1305) performance
1 parent 3587540 commit 96dadc3

File tree

1 file changed

+9
-20
lines changed

1 file changed

+9
-20
lines changed

src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/SpecifyIFormatProvider.cs

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -186,11 +186,11 @@ protected override void InitializeWorker(CompilationStartAnalysisContext context
186186

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

267267
if (!oaContext.Options.IsConfiguredToSkipAnalysis(uiCultureRule, targetMethod, oaContext.ContainingSymbol, oaContext.Compilation))
268268
{
269-
IEnumerable<int> IformatProviderParameterIndices = GetIndexesOfParameterType(targetMethod, iformatProviderType);
270-
foreach (var index in IformatProviderParameterIndices)
269+
foreach (var argument in invocationExpression.Arguments)
271270
{
272-
var argument = invocationExpression.Arguments[index];
273-
274-
if (argument != null && currentUICultureProperty != null &&
275-
installedUICultureProperty != null && currentThreadCurrentUICultureProperty != null)
271+
if (!iformatProviderType.Equals(argument.Parameter?.Type))
276272
{
277-
var semanticModel = argument.SemanticModel!;
278-
279-
var symbol = semanticModel.GetSymbolInfo(argument.Value.Syntax, oaContext.CancellationToken).Symbol;
273+
continue;
274+
}
280275

281-
if (symbol != null &&
276+
if (currentUICultureProperty != null && installedUICultureProperty != null && currentThreadCurrentUICultureProperty != null)
277+
{
278+
if (argument.Value.WalkDownConversion() is IPropertyReferenceOperation { Property: { } symbol } &&
282279
(symbol.Equals(currentUICultureProperty) ||
283280
symbol.Equals(installedUICultureProperty) ||
284281
symbol.Equals(currentThreadCurrentUICultureProperty) ||
@@ -324,14 +321,6 @@ static IEnumerable<IMethodSymbol> GetToStringMethods(INamedTypeSymbol namedTypeS
324321
=> namedTypeSymbol.GetMembers("ToString").OfType<IMethodSymbol>().WhereNotNull();
325322
}
326323

327-
private static IEnumerable<int> GetIndexesOfParameterType(IMethodSymbol targetMethod, INamedTypeSymbol formatProviderType)
328-
{
329-
return targetMethod.Parameters
330-
.Select((Parameter, Index) => (Parameter, Index))
331-
.Where(x => x.Parameter.Type.Equals(formatProviderType))
332-
.Select(x => x.Index);
333-
}
334-
335324
private static ParameterInfo GetParameterInfo(INamedTypeSymbol type, bool isArray = false, int arrayRank = 0, bool isParams = false)
336325
{
337326
return ParameterInfo.GetParameterInfo(type, isArray, arrayRank, isParams);

0 commit comments

Comments
 (0)