Skip to content

Commit

Permalink
Compare symbols instead of method names
Browse files Browse the repository at this point in the history
  • Loading branch information
Youssef1313 committed Dec 22, 2022
1 parent 79cc002 commit 792dd0a
Showing 1 changed file with 10 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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;
}
}
}

0 comments on commit 792dd0a

Please sign in to comment.