diff --git a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/SpecifyCultureForToLowerAndToUpper.cs b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/SpecifyCultureForToLowerAndToUpper.cs index 3134000e9a..4f46e65a35 100644 --- a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/SpecifyCultureForToLowerAndToUpper.cs +++ b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/SpecifyCultureForToLowerAndToUpper.cs @@ -1,6 +1,7 @@ // Copyright (c) Microsoft. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information. using System.Collections.Immutable; +using System.Linq; using Analyzer.Utilities; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.Diagnostics; @@ -33,25 +34,24 @@ public abstract class SpecifyCultureForToLowerAndToUpperAnalyzer : AbstractGloba protected override void InitializeWorker(CompilationStartAnalysisContext context) { + var stringType = context.Compilation.GetSpecialType(SpecialType.System_String); + var toLower = stringType.GetMembers(ToLowerMethodName).FirstOrDefault(m => !m.IsStatic && m is IMethodSymbol method && method.Parameters.IsEmpty); + var toUpper = stringType.GetMembers(ToUpperMethodName).FirstOrDefault(m => !m.IsStatic && m is IMethodSymbol method && method.Parameters.IsEmpty); + if (toLower is null && toUpper is null) + { + return; + } + context.RegisterOperationAction(operationContext => { var operation = (IInvocationOperation)operationContext.Operation; IMethodSymbol methodSymbol = operation.TargetMethod; - if (methodSymbol.ContainingType.SpecialType == SpecialType.System_String && - !methodSymbol.IsStatic && - IsToLowerOrToUpper(methodSymbol.Name) && - //picking the correct overload - methodSymbol.Parameters.Length == 0) + if (methodSymbol.Equals(toLower, SymbolEqualityComparer.Default) || methodSymbol.Equals(toUpper, SymbolEqualityComparer.Default)) { operationContext.ReportDiagnostic(Diagnostic.Create(Rule, GetMethodNameLocation(operation.Syntax))); } }, OperationKind.Invocation); } - - private static bool IsToLowerOrToUpper(string methodName) - { - return methodName == ToLowerMethodName || methodName == ToUpperMethodName; - } } }