Skip to content

Commit

Permalink
Add USP0020 - The Unity runtime invokes Unity messages (#273)
Browse files Browse the repository at this point in the history
  • Loading branch information
sailro authored Mar 3, 2023
1 parent a34ea93 commit 924e526
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 1 deletion.
33 changes: 33 additions & 0 deletions doc/USP0020.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# USP0020 The Unity runtime invokes Unity messages

Unity messages should not be flagged as unused because Unity invokes those messages.

## Suppressed Diagnostic ID

IDE0052 - Private method can be removed as it is never invoked.

## Examples of code that produces a suppressed diagnostic
```csharp
using UnityEngine;

class Camera : MonoBehaviour
{
void Start()
{
//Some code
}

/// <summary>
/// This reference triggers IDE0052 <see cref=""Start"" />
/// </summary>
public float speed = 0f;
}
```

## Why is the diagnostic reported?

The IDE cannot find any references to the method `Start` and believes it to be unused.

## Why do we suppress this diagnostic?

Even though the IDE cannot find any references to `Start`, it will be called by Unity, and should not be removed.
1 change: 1 addition & 0 deletions doc/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,4 @@ ID | Suppressed ID | Justification
[USP0017](USP0017.md) | IDE0074 | Unity objects should not use coalescing assignment
[USP0018](USP0018.md) | IDE0016 | Unity objects should not be used with throw expressions
[USP0019](USP0012.md) | IDE0051 | Don't flag private methods with PreserveAttribute or UsedImplicitlyAttribute as unused
[USP0003](USP0020.md) | IDE0052 | The Unity runtime invokes Unity messages
25 changes: 25 additions & 0 deletions src/Microsoft.Unity.Analyzers.Tests/MessageSuppressorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,31 @@ private void Start()
await VerifyCSharpDiagnosticAsync(test, suppressor);
}

[Fact]
public async Task UnusedMethodWithCrefSuppressed()
{
const string test = @"
using UnityEngine;
public class TestScript : MonoBehaviour
{
private void Start()
{
}
/// <summary>
/// IDE0052 should be suppressed <see cref=""Start"" />
/// </summary>
public float speed = 0f;
}
";

var suppressor = ExpectSuppressor(MessageSuppressor.MethodCrefRule)
.WithLocation(6, 18);

await VerifyCSharpDiagnosticAsync(test, suppressor);
}

[Fact]
public async Task UnusedParameterSuppressed()
{
Expand Down
7 changes: 6 additions & 1 deletion src/Microsoft.Unity.Analyzers/MessageSuppressor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ public class MessageSuppressor : DiagnosticSuppressor
suppressedDiagnosticId: "IDE0051",
justification: Strings.MessageSuppressorJustification);

internal static readonly SuppressionDescriptor MethodCrefRule = new(
id: "USP0020",
suppressedDiagnosticId: "IDE0052",
justification: Strings.MessageSuppressorJustification);

internal static readonly SuppressionDescriptor MethodCodeQualityRule = new(
id: "USP0014",
suppressedDiagnosticId: "CA1822",
Expand All @@ -36,7 +41,7 @@ public class MessageSuppressor : DiagnosticSuppressor
suppressedDiagnosticId: "CA1801",
justification: Strings.MessageSuppressorJustification);

public override ImmutableArray<SuppressionDescriptor> SupportedSuppressions => ImmutableArray.Create(MethodRule, MethodCodeQualityRule, ParameterRule, ParameterCodeQualityRule);
public override ImmutableArray<SuppressionDescriptor> SupportedSuppressions => ImmutableArray.Create(MethodRule, MethodCrefRule, MethodCodeQualityRule, ParameterRule, ParameterCodeQualityRule);

public override void ReportSuppressions(SuppressionAnalysisContext context)
{
Expand Down

0 comments on commit 924e526

Please sign in to comment.