Skip to content

Commit

Permalink
Use more minimalistic version of symbol comparison when nullability i…
Browse files Browse the repository at this point in the history
…s involved
  • Loading branch information
DoctorKrolic committed May 9, 2024
1 parent eb34584 commit bd9cc23
Show file tree
Hide file tree
Showing 11 changed files with 26 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
var compilation = semanticModel.Compilation;

// We can get ParseResult<ActualOptionsType> in case the whole type is aliased, so unwrap options type from ParseResult here
if (optionsType?.OriginalDefinition.Equals(compilation.ParseResultOfTType(), SymbolEqualityComparer.Default) == true)
if (SymbolEqualityComparer.Default.Equals(optionsType?.OriginalDefinition, compilation.ParseResultOfTType()))
{
optionsType = ((INamedTypeSymbol)optionsType).TypeArguments[0];
optionsType = ((INamedTypeSymbol)optionsType!).TypeArguments[0];
}

if (optionsType?.Locations.First() is not { IsInSource: true } optionsTypeSourceLocation)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)

var aliasesAttributeReference = commandHandler
.GetAttributes()
.First(a => a.AttributeClass?.Equals(specialCommandAliasesAttributeType, SymbolEqualityComparer.Default) == true)
.First(a => SymbolEqualityComparer.Default.Equals(a.AttributeClass, specialCommandAliasesAttributeType))
.ApplicationSyntaxReference;

if (aliasesAttributeReference is null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ private static (ArgumentParserInfo? ArgumentParserInfo, OptionsHelpInfo? Options
var comp = context.SemanticModel.Compilation;
var genArgParserAttrType = comp.GetTypeByMetadataName(GeneratedArgumentParserAttributeName)!;

var genArgParserAttrData = context.Attributes.First(a => a.AttributeClass?.Equals(genArgParserAttrType, SymbolEqualityComparer.Default) == true);
var genArgParserAttrData = context.Attributes.First(a => SymbolEqualityComparer.Default.Equals(a.AttributeClass, genArgParserAttrType));
ImmutableArray<SpecialCommandHandlerInfo>.Builder? specialCommandHandlerInfosBuilder = null;

if (genArgParserAttrData.NamedArguments.FirstOrDefault(static n => n.Key == "SpecialCommandHandlers").Value is { IsNull: false, Values: { IsDefault: false } specialCommandHandlers })
Expand All @@ -42,7 +42,7 @@ private static (ArgumentParserInfo? ArgumentParserInfo, OptionsHelpInfo? Options

var aliasesAttrData = commandHandlerType
.GetAttributes()
.FirstOrDefault(a => a.AttributeClass?.Equals(specialCommandAliasesAttributeType, SymbolEqualityComparer.Default) == true);
.FirstOrDefault(a => SymbolEqualityComparer.Default.Equals(a.AttributeClass, specialCommandAliasesAttributeType));

if (aliasesAttrData is null)
{
Expand Down Expand Up @@ -107,7 +107,7 @@ private static (ArgumentParserInfo? ArgumentParserInfo, OptionsHelpInfo? Options

if (optionsType is not INamedTypeSymbol { SpecialType: SpecialType.None, TypeKind: TypeKind.Class or TypeKind.Struct } namedOptionsType ||
!namedOptionsType.Constructors.Any(static c => c.DeclaredAccessibility >= Accessibility.Internal && c.Parameters.IsEmpty) ||
!namedOptionsType.GetAttributes().Any(a => a.AttributeClass?.Equals(comp.OptionsTypeAttributeType(), SymbolEqualityComparer.Default) == true))
!namedOptionsType.GetAttributes().Any(a => SymbolEqualityComparer.Default.Equals(a.AttributeClass, comp.OptionsTypeAttributeType())))
{
return default;
}
Expand Down Expand Up @@ -142,7 +142,7 @@ private static (OptionsInfo? OptionsInfo, OptionsHelpInfo? OptionsHelpInfo) Extr
{
HelpTextGeneratorInfo? helpTextGeneratorInfo = null;

if (optionsType.GetAttributes().FirstOrDefault(a => a.AttributeClass?.Equals(comp.HelpTextGeneratorAttributeType(), SymbolEqualityComparer.Default) == true) is { } helpTextGeneratorAttribute)
if (optionsType.GetAttributes().FirstOrDefault(a => SymbolEqualityComparer.Default.Equals(a.AttributeClass, comp.HelpTextGeneratorAttributeType())) is { } helpTextGeneratorAttribute)
{
var firstArg = helpTextGeneratorAttribute.ConstructorArguments[0];
var secondArg = helpTextGeneratorAttribute.ConstructorArguments[1];
Expand Down Expand Up @@ -547,7 +547,7 @@ private static (EnvironmentInfo EnvironmentInfo, AssemblyVersionInfo AssemblyVer
cancellationToken.ThrowIfCancellationRequested();

var generateDefaultVersionSpecialCommandAttributeType = compilation.GetTypeByMetadataName("ArgumentParsing.SpecialCommands.Version.GenerateDefaultVersionSpecialCommandAttribute");
var forceDefaultVersionCommand = compilation.Assembly.GetAttributes().Any(a => a.AttributeClass?.Equals(generateDefaultVersionSpecialCommandAttributeType, SymbolEqualityComparer.Default) == true);
var forceDefaultVersionCommand = compilation.Assembly.GetAttributes().Any(a => SymbolEqualityComparer.Default.Equals(a.AttributeClass, generateDefaultVersionSpecialCommandAttributeType));

var environmentInfo = new EnvironmentInfo(canUseOptimalSpanBasedAlgorithm, hasStringStartsWithCharOverload, forceDefaultVersionCommand);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ private static void AnalyzeType(SymbolAnalysisContext context, KnownTypes knownT
{
var type = (INamedTypeSymbol)context.Symbol;
var attributes = type.GetAttributes();
var helpTextGeneratorAttribute = attributes.FirstOrDefault(a => a.AttributeClass?.Equals(knownTypes.HelpTextGeneratorAttributeType, SymbolEqualityComparer.Default) == true);
var helpTextGeneratorAttribute = attributes.FirstOrDefault(a => SymbolEqualityComparer.Default.Equals(a.AttributeClass, knownTypes.HelpTextGeneratorAttributeType));
if (helpTextGeneratorAttribute is null)
{
return;
}

if (!attributes.Any(a => a.AttributeClass?.Equals(knownTypes.OptionsTypeAttributeType, SymbolEqualityComparer.Default) == true))
if (!attributes.Any(a => SymbolEqualityComparer.Default.Equals(a.AttributeClass, knownTypes.OptionsTypeAttributeType)))
{
context.ReportDiagnostic(
Diagnostic.Create(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,12 @@ private static void AnalyzeOptionsType(SymbolAnalysisContext context, LanguageVe
var optionsType = (INamedTypeSymbol)context.Symbol;
var attributes = optionsType.GetAttributes();

if (!attributes.Any(a => a.AttributeClass?.Equals(knownTypes.OptionsTypeAttributeType, SymbolEqualityComparer.Default) == true))
if (!attributes.Any(a => SymbolEqualityComparer.Default.Equals(a.AttributeClass, knownTypes.OptionsTypeAttributeType)))
{
return;
}

var helpTextGeneratorAttribute = attributes.FirstOrDefault(a => a.AttributeClass?.Equals(knownTypes.HelpTextGeneratorAttributeType, SymbolEqualityComparer.Default) == true);
var helpTextGeneratorAttribute = attributes.FirstOrDefault(a => SymbolEqualityComparer.Default.Equals(a.AttributeClass, knownTypes.HelpTextGeneratorAttributeType));
if (helpTextGeneratorAttribute is not null)
{
var firstArg = helpTextGeneratorAttribute.ConstructorArguments[0];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ private static void AnalyzeProperty(SymbolAnalysisContext context, KnownTypes kn
var property = (IPropertySymbol)context.Symbol;

if (!property.GetAttributes()
.Any(a => a.AttributeClass?.Equals(knownTypes.OptionAttributeType, SymbolEqualityComparer.Default) == true ||
a.AttributeClass?.Equals(knownTypes.ParameterAttributeType, SymbolEqualityComparer.Default) == true ||
a.AttributeClass?.Equals(knownTypes.RemainingParametersAttributeType, SymbolEqualityComparer.Default) == true))
.Any(a => SymbolEqualityComparer.Default.Equals(a.AttributeClass, knownTypes.OptionAttributeType) ||
SymbolEqualityComparer.Default.Equals(a.AttributeClass, knownTypes.ParameterAttributeType) ||
SymbolEqualityComparer.Default.Equals(a.AttributeClass, knownTypes.RemainingParametersAttributeType)))
{
return;
}

if (property.ContainingType.GetAttributes()
.Any(a => a.AttributeClass?.Equals(knownTypes.OptionsTypeAttributeType, SymbolEqualityComparer.Default) == true))
.Any(a => SymbolEqualityComparer.Default.Equals(a.AttributeClass, knownTypes.OptionsTypeAttributeType)))
{
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ private static void AnalyzeParserSignature(SymbolAnalysisContext context, KnownT
var method = (IMethodSymbol)context.Symbol;

if (method.GetAttributes()
.FirstOrDefault(a => a.AttributeClass?.Equals(knownTypes.GeneratedArgumentParserAttributeType, SymbolEqualityComparer.Default) == true) is not { } generatedArgParserAttrData)
.FirstOrDefault(a => SymbolEqualityComparer.Default.Equals(a.AttributeClass, knownTypes.GeneratedArgumentParserAttributeType)) is not { } generatedArgParserAttrData)
{
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ private static void AnalyzeSpecialCommandHandler(SymbolAnalysisContext context,
diagnosticLocation));
}

var aliasesAttribute = type.GetAttributes().FirstOrDefault(a => a.AttributeClass?.Equals(knownTypes.SpecialCommandAliasesAttributeType, SymbolEqualityComparer.Default) == true);
var aliasesAttribute = type.GetAttributes().FirstOrDefault(a => SymbolEqualityComparer.Default.Equals(a.AttributeClass, knownTypes.SpecialCommandAliasesAttributeType));
var constructorArg = aliasesAttribute?.ConstructorArguments[0];

if (aliasesAttribute is null ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ public override void ReportSuppressions(SuppressionAnalysisContext context)

var compilation = semanticModel.Compilation;

if (!propertySymbol.ContainingType.GetAttributes().Any(a => a.AttributeClass?.Equals(compilation.OptionsTypeAttributeType(), SymbolEqualityComparer.Default) == true))
if (!propertySymbol.ContainingType.GetAttributes().Any(a => SymbolEqualityComparer.Default.Equals(a.AttributeClass, compilation.OptionsTypeAttributeType())))
{
continue;
}

if (propertySymbol.GetAttributes().Any(a => a.AttributeClass?.Equals(compilation.OptionAttributeType(), SymbolEqualityComparer.Default) == true ||
a.AttributeClass?.Equals(compilation.ParameterAttributeType(), SymbolEqualityComparer.Default) == true ||
a.AttributeClass?.Equals(compilation.RemainingParametersAttributeType(), SymbolEqualityComparer.Default) == true))
if (propertySymbol.GetAttributes().Any(a => SymbolEqualityComparer.Default.Equals(a.AttributeClass, compilation.OptionAttributeType()) ||
SymbolEqualityComparer.Default.Equals(a.AttributeClass, compilation.ParameterAttributeType()) ||
SymbolEqualityComparer.Default.Equals(a.AttributeClass, compilation.RemainingParametersAttributeType())))
{
context.ReportSuppression(Suppression.Create(SuppressionDescriptors.ParserRelatedPropertyMarkedWithRequiredAttributeInOptionsType, diagnostic));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ public override void ReportSuppressions(SuppressionAnalysisContext context)

var compilation = semanticModel.Compilation;

if (!propertySymbol.ContainingType.GetAttributes().Any(a => a.AttributeClass?.Equals(compilation.OptionsTypeAttributeType(), SymbolEqualityComparer.Default) == true))
if (!propertySymbol.ContainingType.GetAttributes().Any(a => SymbolEqualityComparer.Default.Equals(a.AttributeClass, compilation.OptionsTypeAttributeType())))
{
continue;
}

if (!propertySymbol.GetAttributes().Any(a => a.AttributeClass?.Equals(compilation.RemainingParametersAttributeType(), SymbolEqualityComparer.Default) == true))
if (!propertySymbol.GetAttributes().Any(a => SymbolEqualityComparer.Default.Equals(a.AttributeClass, compilation.RemainingParametersAttributeType())))
{
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ public override void ReportSuppressions(SuppressionAnalysisContext context)

var compilation = semanticModel.Compilation;

if (!propertySymbol.ContainingType.GetAttributes().Any(a => a.AttributeClass?.Equals(compilation.OptionsTypeAttributeType(), SymbolEqualityComparer.Default) == true))
if (!propertySymbol.ContainingType.GetAttributes().Any(a => SymbolEqualityComparer.Default.Equals(a.AttributeClass, compilation.OptionsTypeAttributeType())))
{
continue;
}

if (!propertySymbol.GetAttributes().Any(a => a.AttributeClass?.Equals(compilation.OptionAttributeType(), SymbolEqualityComparer.Default) == true))
if (!propertySymbol.GetAttributes().Any(a => SymbolEqualityComparer.Default.Equals(a.AttributeClass, compilation.OptionAttributeType())))
{
continue;
}
Expand Down

0 comments on commit bd9cc23

Please sign in to comment.