Skip to content

Commit

Permalink
Feedback and some refactorings
Browse files Browse the repository at this point in the history
  • Loading branch information
DoctorKrolic committed Apr 29, 2024
1 parent aba7cf1 commit 37a5a32
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 15 deletions.
Expand Up @@ -31,7 +31,11 @@ public class CompareSymbolsCorrectlyAnalyzer : DiagnosticAnalyzer
public const string SymbolEqualityComparerName = "Microsoft.CodeAnalysis.SymbolEqualityComparer";
public const string RulePropertyName = "Rule";

public static readonly DiagnosticDescriptor EqualityRule = new(
public const string EqualityRuleName = "EqualityRule";
public const string GetHashCodeRuleName = "GetHashCodeRule";
public const string CollectionRuleName = "CollectionRule";

private static readonly DiagnosticDescriptor s_equalityRule = new(
DiagnosticIds.CompareSymbolsCorrectlyRuleId,
s_localizableTitle,
s_localizableMessage,
Expand All @@ -41,7 +45,7 @@ public class CompareSymbolsCorrectlyAnalyzer : DiagnosticAnalyzer
description: s_localizableDescription,
customTags: WellKnownDiagnosticTagsExtensions.Telemetry);

public static readonly DiagnosticDescriptor GetHashCodeRule = new(
private static readonly DiagnosticDescriptor s_getHashCodeRule = new(
DiagnosticIds.CompareSymbolsCorrectlyRuleId,
s_localizableTitle,
s_localizableMessage,
Expand All @@ -51,7 +55,7 @@ public class CompareSymbolsCorrectlyAnalyzer : DiagnosticAnalyzer
description: CreateLocalizableResourceString(nameof(CompareSymbolsCorrectlyDescriptionGetHashCode)),
customTags: WellKnownDiagnosticTagsExtensions.Telemetry);

public static readonly DiagnosticDescriptor CollectionRule = new(
private static readonly DiagnosticDescriptor s_collectionRule = new(
DiagnosticIds.CompareSymbolsCorrectlyRuleId,
s_localizableTitle,
s_localizableMessage,
Expand All @@ -61,7 +65,16 @@ public class CompareSymbolsCorrectlyAnalyzer : DiagnosticAnalyzer
description: s_localizableDescription,
customTags: WellKnownDiagnosticTagsExtensions.Telemetry);

public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create(EqualityRule);
private static readonly ImmutableDictionary<string, string?> s_EqualityRuleProperties =
ImmutableDictionary.CreateRange([new KeyValuePair<string, string?>(RulePropertyName, EqualityRuleName)]);

private static readonly ImmutableDictionary<string, string?> s_GetHashCodeRuleProperties =
ImmutableDictionary.CreateRange([new KeyValuePair<string, string?>(RulePropertyName, GetHashCodeRuleName)]);

private static readonly ImmutableDictionary<string, string?> s_CollectionRuleProperties =
ImmutableDictionary.CreateRange([new KeyValuePair<string, string?>(RulePropertyName, CollectionRuleName)]);

public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create(s_equalityRule);

public override void Initialize(AnalysisContext context)
{
Expand Down Expand Up @@ -145,7 +158,7 @@ private static void HandleBinaryOperator(in OperationAnalysisContext context, IN
return;
}

context.ReportDiagnostic(binary.Syntax.GetLocation().CreateDiagnostic(EqualityRule, MakeProperties(nameof(EqualityRule))));
context.ReportDiagnostic(binary.Syntax.GetLocation().CreateDiagnostic(s_equalityRule, s_EqualityRuleProperties));
}

private static void HandleInvocationOperation(
Expand All @@ -166,7 +179,7 @@ private static void HandleBinaryOperator(in OperationAnalysisContext context, IN
// without the correct arguments
if (IsSymbolType(invocationOperation.Instance, symbolType))
{
context.ReportDiagnostic(invocationOperation.CreateDiagnostic(GetHashCodeRule, MakeProperties(nameof(GetHashCode))));
context.ReportDiagnostic(invocationOperation.CreateDiagnostic(s_getHashCodeRule, s_GetHashCodeRuleProperties));
}

break;
Expand All @@ -177,7 +190,7 @@ private static void HandleBinaryOperator(in OperationAnalysisContext context, IN
var parameters = invocationOperation.Arguments;
if (parameters.All(p => IsSymbolType(p.Value, symbolType)))
{
context.ReportDiagnostic(invocationOperation.Syntax.GetLocation().CreateDiagnostic(EqualityRule, MakeProperties(nameof(EqualityRule))));
context.ReportDiagnostic(invocationOperation.Syntax.GetLocation().CreateDiagnostic(s_equalityRule, s_EqualityRuleProperties));
}
}

Expand All @@ -190,7 +203,7 @@ private static void HandleBinaryOperator(in OperationAnalysisContext context, IN
systemHashCodeType.Equals(method.ContainingType, SymbolEqualityComparer.Default) &&
invocationOperation.Arguments.Any(arg => IsSymbolType(arg.Value, symbolType)))
{
context.ReportDiagnostic(invocationOperation.CreateDiagnostic(GetHashCodeRule, MakeProperties(nameof(GetHashCodeRule))));
context.ReportDiagnostic(invocationOperation.CreateDiagnostic(s_getHashCodeRule, s_GetHashCodeRuleProperties));
}

break;
Expand All @@ -202,7 +215,7 @@ private static void HandleBinaryOperator(in OperationAnalysisContext context, IN
IsBehavingOnSymbolType(method, symbolType) &&
!invocationOperation.Arguments.Any(arg => IsSymbolType(arg.Value, iEqualityComparer)))
{
context.ReportDiagnostic(invocationOperation.CreateDiagnostic(CollectionRule, MakeProperties(nameof(CollectionRule))));
context.ReportDiagnostic(invocationOperation.CreateDiagnostic(s_collectionRule, s_CollectionRuleProperties));
}

break;
Expand Down Expand Up @@ -252,7 +265,7 @@ static bool IsBehavingOnSymbolType(IMethodSymbol? method, INamedTypeSymbol symbo
IsSymbolType(createdType.TypeArguments[0], symbolType) &&
!objectCreation.Arguments.Any(arg => IsSymbolType(arg.Value, iEqualityComparerType)))
{
context.ReportDiagnostic(objectCreation.CreateDiagnostic(CollectionRule, MakeProperties(nameof(CollectionRule))));
context.ReportDiagnostic(objectCreation.CreateDiagnostic(s_collectionRule, s_CollectionRuleProperties));
}
}

Expand Down Expand Up @@ -357,9 +370,6 @@ void AddOrUpdate(string methodName, INamedTypeSymbol typeSymbol)
}
}

private static ImmutableDictionary<string, string?> MakeProperties(string rule)
=> ImmutableDictionary.CreateRange([new KeyValuePair<string, string?>(RulePropertyName, rule)]);

public static bool UseSymbolEqualityComparer(Compilation compilation)
=> compilation.GetOrCreateTypeByMetadataName(SymbolEqualityComparerName) is object;
}
Expand Down
Expand Up @@ -36,15 +36,15 @@ public sealed override Task RegisterCodeFixesAsync(CodeFixContext context)
{
switch (rule)
{
case nameof(CompareSymbolsCorrectlyAnalyzer.EqualityRule):
case CompareSymbolsCorrectlyAnalyzer.EqualityRuleName:
context.RegisterCodeFix(
CodeAction.Create(
CodeAnalysisDiagnosticsResources.CompareSymbolsCorrectlyCodeFix,
cancellationToken => ConvertToEqualsAsync(context.Document, diagnostic.Location.SourceSpan, cancellationToken),
equivalenceKey: nameof(CompareSymbolsCorrectlyFix)),
diagnostic);
break;
case nameof(CompareSymbolsCorrectlyAnalyzer.CollectionRule):
case CompareSymbolsCorrectlyAnalyzer.CollectionRuleName:
context.RegisterCodeFix(
CodeAction.Create(
CodeAnalysisDiagnosticsResources.CompareSymbolsCorrectlyCodeFix,
Expand Down

0 comments on commit 37a5a32

Please sign in to comment.