From d52dec909734d21cc466adadee021cbbf4a07476 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 22 Oct 2024 22:02:26 -0700 Subject: [PATCH] Fix suppressions for custom attribute targets --- ...ttributeWithSupportedTargetDiagnosticSuppressor.cs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/CommunityToolkit.Mvvm.SourceGenerators/Diagnostics/Suppressors/ObservablePropertyAttributeWithSupportedTargetDiagnosticSuppressor.cs b/src/CommunityToolkit.Mvvm.SourceGenerators/Diagnostics/Suppressors/ObservablePropertyAttributeWithSupportedTargetDiagnosticSuppressor.cs index 0b9b4246d..57bae864b 100644 --- a/src/CommunityToolkit.Mvvm.SourceGenerators/Diagnostics/Suppressors/ObservablePropertyAttributeWithSupportedTargetDiagnosticSuppressor.cs +++ b/src/CommunityToolkit.Mvvm.SourceGenerators/Diagnostics/Suppressors/ObservablePropertyAttributeWithSupportedTargetDiagnosticSuppressor.cs @@ -55,7 +55,16 @@ public override void ReportSuppressions(SuppressionAnalysisContext context) semanticModel.Compilation.GetTypeByMetadataName("CommunityToolkit.Mvvm.ComponentModel.ObservablePropertyAttribute") is INamedTypeSymbol observablePropertySymbol && fieldSymbol.HasAttributeWithType(observablePropertySymbol)) { - context.ReportSuppression(Suppression.Create(PropertyAttributeListForObservablePropertyField, diagnostic)); + // Emit the right suppression based on the attribute modifier. For 'property:', Roslyn + // will emit the 'CS0657' warning, whereas for 'get:' or 'set:', it will emit 'CS0658'. + if (attributeTarget.Identifier.IsKind(SyntaxKind.PropertyKeyword)) + { + context.ReportSuppression(Suppression.Create(PropertyAttributeListForObservablePropertyField, diagnostic)); + } + else + { + context.ReportSuppression(Suppression.Create(PropertyAttributeListForObservablePropertyFieldAccessors, diagnostic)); + } } } }