-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add USP0019 Don't flag private methods decorated with `PreserveAttrib…
…ute` or `UsedImplicitlyAttribute` as unused (#214) * Add USP0019 Don't flag private methods decorated with PreserveAttribute or UsedImplicitlyAttribute as unused * Not needed yet * indent * Refactor
- Loading branch information
Showing
7 changed files
with
189 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# USP0019 Don't flag private methods decorated with PreserveAttribute or UsedImplicitlyAttribute as unused | ||
|
||
Methods decorated with `PreserveAttribute` or `UsedImplicitlyAttribute` attributes are not unused. | ||
|
||
## Suppressed Diagnostic ID | ||
|
||
IDE0051 - Remove unused private members | ||
|
||
## Examples of code that produces a suppressed diagnostic | ||
```csharp | ||
using UnityEngine; | ||
using UnityEgine.Scripting; | ||
|
||
class Loader | ||
{ | ||
[PreserveAttribute] | ||
private void InvokeMe() | ||
{ | ||
} | ||
|
||
public string Name; // "InvokeMe" serialized | ||
private void Update() { | ||
Invoke(Name, 0); | ||
} | ||
} | ||
``` | ||
|
||
## Why is the diagnostic reported? | ||
|
||
The IDE cannot find any references to the method `InvokeMe` and believes it to be unused. | ||
|
||
## Why do we suppress this diagnostic? | ||
|
||
Even though the IDE cannot find any references to `InvokeMe` , it will be called by Unity, and should not be removed. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
src/Microsoft.Unity.Analyzers.Tests/ImplicitUsageAttributeSuppressorTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/*-------------------------------------------------------------------------------------------- | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See LICENSE in the project root for license information. | ||
*-------------------------------------------------------------------------------------------*/ | ||
|
||
using System.Threading.Tasks; | ||
using Xunit; | ||
|
||
namespace Microsoft.Unity.Analyzers.Tests; | ||
|
||
public class ImplicitUsageAttributeSuppressorTests : BaseSuppressorVerifierTest<ImplicitUsageAttributeSuppressor> | ||
{ | ||
[Fact] | ||
public async Task UnityPreserveTest() | ||
{ | ||
const string test = @" | ||
using UnityEngine; | ||
using UnityEngine.Scripting; | ||
class Camera : MonoBehaviour | ||
{ | ||
[Preserve] | ||
private void Foo() { | ||
} | ||
} | ||
"; | ||
|
||
var suppressor = ExpectSuppressor(ImplicitUsageAttributeSuppressor.Rule) | ||
.WithLocation(8, 18); | ||
|
||
await VerifyCSharpDiagnosticAsync(test, suppressor); | ||
} | ||
|
||
[Fact] | ||
public async Task OwnPreserveTest() | ||
{ | ||
const string test = @" | ||
using UnityEngine; | ||
class Camera : MonoBehaviour | ||
{ | ||
[My.Own.Stuff.Preserve] | ||
private void Foo() { | ||
} | ||
} | ||
namespace My.Own.Stuff { | ||
public class PreserveAttribute : System.Attribute { } | ||
} | ||
"; | ||
|
||
var suppressor = ExpectSuppressor(ImplicitUsageAttributeSuppressor.Rule) | ||
.WithLocation(7, 18); | ||
|
||
await VerifyCSharpDiagnosticAsync(test, suppressor); | ||
} | ||
|
||
[Fact] | ||
public async Task UsedImplicitlyTest() | ||
{ | ||
const string test = @" | ||
using UnityEngine; | ||
using JetBrains.Annotations; | ||
class Camera : MonoBehaviour | ||
{ | ||
[UsedImplicitly] | ||
private void Foo() { | ||
} | ||
} | ||
"; | ||
|
||
var suppressor = ExpectSuppressor(ImplicitUsageAttributeSuppressor.Rule) | ||
.WithLocation(8, 18); | ||
|
||
await VerifyCSharpDiagnosticAsync(test, suppressor); | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
src/Microsoft.Unity.Analyzers/ImplicitUsageAttributeSuppressor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/*-------------------------------------------------------------------------------------------- | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See LICENSE in the project root for license information. | ||
*-------------------------------------------------------------------------------------------*/ | ||
|
||
using System.Collections.Immutable; | ||
using System.Linq; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
using Microsoft.Unity.Analyzers.Resources; | ||
|
||
namespace Microsoft.Unity.Analyzers; | ||
|
||
[DiagnosticAnalyzer(LanguageNames.CSharp)] | ||
public class ImplicitUsageAttributeSuppressor : DiagnosticSuppressor | ||
{ | ||
internal static readonly SuppressionDescriptor Rule = new( | ||
id: "USP0019", | ||
suppressedDiagnosticId: "IDE0051", | ||
justification: Strings.ImplicitUsageAttributeSuppressorJustification); | ||
|
||
public override void ReportSuppressions(SuppressionAnalysisContext context) | ||
{ | ||
foreach (var diagnostic in context.ReportedDiagnostics) | ||
{ | ||
AnalyzeDiagnostic(diagnostic, context); | ||
} | ||
} | ||
|
||
public override ImmutableArray<SuppressionDescriptor> SupportedSuppressions => ImmutableArray.Create(Rule); | ||
|
||
private void AnalyzeDiagnostic(Diagnostic diagnostic, SuppressionAnalysisContext context) | ||
{ | ||
var methodDeclarationSyntax = context.GetSuppressibleNode<MethodDeclarationSyntax>(diagnostic); | ||
if (methodDeclarationSyntax == null) | ||
return; | ||
|
||
var model = context.GetSemanticModel(diagnostic.Location.SourceTree); | ||
if (model.GetDeclaredSymbol(methodDeclarationSyntax) is not IMethodSymbol methodSymbol) | ||
return; | ||
|
||
if (!IsSuppressable(methodSymbol)) | ||
return; | ||
|
||
context.ReportSuppression(Suppression.Create(Rule, diagnostic)); | ||
} | ||
|
||
private bool IsSuppressable(IMethodSymbol methodSymbol) | ||
{ | ||
// The Unity code stripper will consider any attribute with the exact name "PreserveAttribute", regardless of the namespace or assembly | ||
return methodSymbol.GetAttributes().Any(a => a.AttributeClass.Matches(typeof(JetBrains.Annotations.UsedImplicitlyAttribute)) || a.AttributeClass.Name == nameof(UnityEngine.Scripting.PreserveAttribute)); | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters