diff --git a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/SpecifyIFormatProvider.cs b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/SpecifyIFormatProvider.cs index 158e80fea5..d634edbc7c 100644 --- a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/SpecifyIFormatProvider.cs +++ b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/SpecifyIFormatProvider.cs @@ -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[]). @@ -266,19 +266,16 @@ protected override void InitializeWorker(CompilationStartAnalysisContext context if (!oaContext.Options.IsConfiguredToSkipAnalysis(uiCultureRule, targetMethod, oaContext.ContainingSymbol, oaContext.Compilation)) { - IEnumerable 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) || @@ -324,14 +321,6 @@ static IEnumerable GetToStringMethods(INamedTypeSymbol namedTypeS => namedTypeSymbol.GetMembers("ToString").OfType().WhereNotNull(); } - private static IEnumerable 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);