Skip to content

Commit

Permalink
Do base type check early in ClassUsingAttributeInsteadOfInheritanceAn…
Browse files Browse the repository at this point in the history
…alyzer
  • Loading branch information
Sergio0694 committed Jan 29, 2023
1 parent 9be7c06 commit 941fcbd
Showing 1 changed file with 10 additions and 13 deletions.
Expand Up @@ -68,7 +68,7 @@ public override void Initialize(AnalysisContext context)
context.RegisterSymbolAction(context =>
{
// We're looking for class declarations that don't have any base type
if (context.Symbol is not INamedTypeSymbol { TypeKind: TypeKind.Class, IsRecord: false, IsStatic: false, IsImplicitlyDeclared: false } classSymbol)
if (context.Symbol is not INamedTypeSymbol { TypeKind: TypeKind.Class, IsRecord: false, IsStatic: false, IsImplicitlyDeclared: false, BaseType.SpecialType: SpecialType.System_Object } classSymbol)
{
return;
}
Expand All @@ -80,18 +80,15 @@ public override void Initialize(AnalysisContext context)
typeSymbols.TryGetValue(attributeName, out INamedTypeSymbol? attributeSymbol) &&
SymbolEqualityComparer.Default.Equals(attributeClass, attributeSymbol))
{
// The type is annotated with either [ObservableObject] or [INotifyPropertyChanged].
if (classSymbol.BaseType is { SpecialType: SpecialType.System_Object })
{
// This type is using the attribute when it could just inherit from ObservableObject, which is preferred
context.ReportDiagnostic(Diagnostic.Create(
GeneratorAttributeNamesToDiagnosticsMap[attributeClass.Name],
context.Symbol.Locations.FirstOrDefault(),
ImmutableDictionary.Create<string, string?>()
.Add(TypeNameKey, classSymbol.Name)
.Add(AttributeTypeNameKey, attributeName),
context.Symbol));
}
// The type is annotated with either [ObservableObject] or [INotifyPropertyChanged], and we already validated
// that it has no other base type, so emit a diagnostic to suggest inheriting from ObservableObject instead.
context.ReportDiagnostic(Diagnostic.Create(
GeneratorAttributeNamesToDiagnosticsMap[attributeClass.Name],
context.Symbol.Locations.FirstOrDefault(),
ImmutableDictionary.Create<string, string?>()
.Add(TypeNameKey, classSymbol.Name)
.Add(AttributeTypeNameKey, attributeName),
context.Symbol));
}
}
}, SymbolKind.NamedType);
Expand Down

0 comments on commit 941fcbd

Please sign in to comment.