Skip to content

Commit 9c0b241

Browse files
committed
Update diagnostic suppressor for forwarded attributes
1 parent 9f99dd1 commit 9c0b241

File tree

3 files changed

+14
-6
lines changed

3 files changed

+14
-6
lines changed

src/CommunityToolkit.Mvvm.SourceGenerators/CommunityToolkit.Mvvm.SourceGenerators.projitems

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
<Compile Include="$(MSBuildThisFileDirectory)Diagnostics\Analyzers\FieldReferenceForObservablePropertyFieldAnalyzer.cs" />
4949
<Compile Include="$(MSBuildThisFileDirectory)Diagnostics\Analyzers\UnsupportedCSharpLanguageVersionAnalyzer.cs" />
5050
<Compile Include="$(MSBuildThisFileDirectory)Diagnostics\Suppressors\RelayCommandAttributeWithFieldOrPropertyTargetDiagnosticSuppressor.cs" />
51-
<Compile Include="$(MSBuildThisFileDirectory)Diagnostics\Suppressors\ObservablePropertyAttributeWithPropertyTargetDiagnosticSuppressor.cs" />
51+
<Compile Include="$(MSBuildThisFileDirectory)Diagnostics\Suppressors\ObservablePropertyAttributeWithSupportedTargetDiagnosticSuppressor.cs" />
5252
<Compile Include="$(MSBuildThisFileDirectory)Diagnostics\DiagnosticDescriptors.cs" />
5353
<Compile Include="$(MSBuildThisFileDirectory)Diagnostics\SuppressionDescriptors.cs" />
5454
<Compile Include="$(MSBuildThisFileDirectory)Extensions\AttributeDataExtensions.cs" />

src/CommunityToolkit.Mvvm.SourceGenerators/Diagnostics/SuppressionDescriptors.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,15 @@ internal static class SuppressionDescriptors
1717
public static readonly SuppressionDescriptor PropertyAttributeListForObservablePropertyField = new(
1818
id: "MVVMTKSPR0001",
1919
suppressedDiagnosticId: "CS0657",
20-
justification: "Fields using [ObservableProperty] can use [property:] attribute lists to forward attributes to the generated properties");
20+
justification: "Fields using [ObservableProperty] can use [property:], [set:] and [set:] attribute lists to forward attributes to the generated properties");
21+
22+
/// <summary>
23+
/// Gets a <see cref="SuppressionDescriptor"/> for a field using [ObservableProperty] with an attribute list targeting a get or set accessor.
24+
/// </summary>
25+
public static readonly SuppressionDescriptor PropertyAttributeListForObservablePropertyFieldAccessors = new(
26+
id: "MVVMTKSPR0001",
27+
suppressedDiagnosticId: "CS0658",
28+
justification: "Fields using [ObservableProperty] can use [property:], [set:] and [set:] attribute lists to forward attributes to the generated properties");
2129

2230
/// <summary>
2331
/// Gets a <see cref="SuppressionDescriptor"/> for a method using [RelayCommand] with an attribute list targeting a field or property.
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace CommunityToolkit.Mvvm.SourceGenerators;
1414

1515
/// <summary>
1616
/// <para>
17-
/// A diagnostic suppressor to suppress CS0657 warnings for fields with [ObservableProperty] using a [property:] attribute list.
17+
/// A diagnostic suppressor to suppress CS0657 warnings for fields with [ObservableProperty] using a [property:] attribute list (or [set:] or [get:]).
1818
/// </para>
1919
/// <para>
2020
/// That is, this diagnostic suppressor will suppress the following diagnostic:
@@ -29,10 +29,10 @@ namespace CommunityToolkit.Mvvm.SourceGenerators;
2929
/// </para>
3030
/// </summary>
3131
[DiagnosticAnalyzer(LanguageNames.CSharp)]
32-
public sealed class ObservablePropertyAttributeWithPropertyTargetDiagnosticSuppressor : DiagnosticSuppressor
32+
public sealed class ObservablePropertyAttributeWithSupportedTargetDiagnosticSuppressor : DiagnosticSuppressor
3333
{
3434
/// <inheritdoc/>
35-
public override ImmutableArray<SuppressionDescriptor> SupportedSuppressions => ImmutableArray.Create(PropertyAttributeListForObservablePropertyField);
35+
public override ImmutableArray<SuppressionDescriptor> SupportedSuppressions => ImmutableArray.Create(PropertyAttributeListForObservablePropertyField, PropertyAttributeListForObservablePropertyFieldAccessors);
3636

3737
/// <inheritdoc/>
3838
public override void ReportSuppressions(SuppressionAnalysisContext context)
@@ -43,7 +43,7 @@ public override void ReportSuppressions(SuppressionAnalysisContext context)
4343

4444
// Check that the target is effectively [property:] over a field declaration with at least one variable, which is the only case we are interested in
4545
if (syntaxNode is AttributeTargetSpecifierSyntax { Parent.Parent: FieldDeclarationSyntax { Declaration.Variables.Count: > 0 } fieldDeclaration } attributeTarget &&
46-
attributeTarget.Identifier.IsKind(SyntaxKind.PropertyKeyword))
46+
(attributeTarget.Identifier.IsKind(SyntaxKind.PropertyKeyword) || attributeTarget.Identifier.IsKind(SyntaxKind.GetKeyword) || attributeTarget.Identifier.IsKind(SyntaxKind.SetKeyword)))
4747
{
4848
SemanticModel semanticModel = context.GetSemanticModel(syntaxNode.SyntaxTree);
4949

0 commit comments

Comments
 (0)