Skip to content

Add support for CreateAttribute #372

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,27 @@ class Camera : MonoBehaviour
[SerializeField]
string someField = ""default"";
}
";
var context = AnalyzerVerificationContext.Default
.WithAnalyzerOption("dotnet_style_readonly_field", "false");

var suppressor = ExpectSuppressor(SerializeFieldSuppressor.UnusedRule)
.WithLocation(7, 12);

await VerifyCSharpDiagnosticAsync(context, test, suppressor);
}

[Fact]
public async Task PrivateFieldWithCreateAttributeUnusedSuppressed()
{
const string test = @"
using UnityEngine;

class Camera : MonoBehaviour
{
[Unity.Properties.CreateProperty]
string someField = ""default"";
}
";
var context = AnalyzerVerificationContext.Default
.WithAnalyzerOption("dotnet_style_readonly_field", "false");
Expand Down
18 changes: 16 additions & 2 deletions src/Microsoft.Unity.Analyzers/SerializeFieldSuppressor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
* Licensed under the MIT License. See LICENSE in the project root for license information.
*-------------------------------------------------------------------------------------------*/

using System;
using System.Collections.Immutable;
using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.Unity.Analyzers.Resources;
using Unity.Properties;
using UnityEngine;

namespace Microsoft.Unity.Analyzers;
Expand Down Expand Up @@ -36,6 +38,8 @@ public class SerializeFieldSuppressor : DiagnosticSuppressor
suppressedDiagnosticId: "CS0649",
justification: Strings.NeverAssignedSerializeFieldSuppressorJustification);

private static readonly Type[] _suppressableAttributeTypes = [typeof(SerializeField), typeof(SerializeReference), typeof(CreatePropertyAttribute)];

public override ImmutableArray<SuppressionDescriptor> SupportedSuppressions => ImmutableArray.Create(ReadonlyRule, UnusedRule, UnusedCodeQualityRule, NeverAssignedRule);

public override void ReportSuppressions(SuppressionAnalysisContext context)
Expand All @@ -46,12 +50,22 @@ public override void ReportSuppressions(SuppressionAnalysisContext context)
}
}

private static bool IsSuppressableAttribute(INamedTypeSymbol? symbol, Type type)
{
return symbol != null && symbol.Matches(type);
}

private static bool IsSuppressableAttribute(INamedTypeSymbol? symbol)
{
return _suppressableAttributeTypes.Any(type => IsSuppressableAttribute(symbol, type));
}

private static bool IsSuppressable(IFieldSymbol fieldSymbol)
{
if (fieldSymbol.GetAttributes().Any(a => a.AttributeClass != null && (a.AttributeClass.Matches(typeof(SerializeField)) || a.AttributeClass.Matches(typeof(SerializeReference)))))
if (fieldSymbol.GetAttributes().Any(a => IsSuppressableAttribute(a.AttributeClass)))
return true;

if (fieldSymbol.DeclaredAccessibility == Accessibility.Public && fieldSymbol.ContainingType.Extends(typeof(Object)))
if (fieldSymbol.DeclaredAccessibility == Accessibility.Public && fieldSymbol.ContainingType.Extends(typeof(UnityEngine.Object)))
return true;

return false;
Expand Down
5 changes: 5 additions & 0 deletions src/Microsoft.Unity.Analyzers/UnityStubs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -796,6 +796,11 @@ struct TransformAccess
}
}

namespace Unity.Properties
{
class CreatePropertyAttribute : System.Attribute { }
}

namespace JetBrains.Annotations
{
class UsedImplicitlyAttribute : Attribute { }
Expand Down