Skip to content
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

Fix UNT0006 when a Unity message is wrongly referenced. #363

Merged
merged 1 commit into from
Nov 22, 2024
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
30 changes: 29 additions & 1 deletion src/Microsoft.Unity.Analyzers.Tests/MessageSignatureTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ bool OnPreGeneratingCSProjectFiles(int foo)
[Fact]
public async Task MessageSignatureUnityLogic()
{
// Unity allows to specify less parameters if you don't need them
// Unity allows to specify fewer parameters if you don't need them
const string test = @"
using UnityEngine;

Expand Down Expand Up @@ -381,4 +381,32 @@ static async Awaitable<bool> OnPreGeneratingCSProjectFiles()
await VerifyCSharpFixAsync(test, fixedTest);
}

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

class Camera : Editor
{
private void Foo()
{
OnSceneGUI(null);
}

private void OnSceneGUI(object foo)
{
}
}
";

var diagnostic = ExpectDiagnostic()
.WithLocation(11, 18)
.WithArguments("OnSceneGUI");

await VerifyCSharpDiagnosticAsync(test, diagnostic);

// In this special case, we do not provide a codefix, given it would break the code as the message is -wrongly- used elsewhere
await VerifyCSharpFixAsync(test, test);
}
}
15 changes: 15 additions & 0 deletions src/Microsoft.Unity.Analyzers/CodeFixContextExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.FindSymbols;

namespace Microsoft.Unity.Analyzers;

Expand All @@ -31,4 +32,18 @@ internal static class CodeFixContextExtensions
.OfType<T>()
.FirstOrDefault(predicate);
}

public static async Task<bool> IsReferencedAsync(this CodeFixContext context, SyntaxNode declaration)
{
var semanticModel = await context.Document
.GetSemanticModelAsync(context.CancellationToken)
.ConfigureAwait(false);

var symbol = semanticModel?.GetDeclaredSymbol(declaration);
if (symbol == null)
return false;

var references = await SymbolFinder.FindReferencesAsync(symbol, context.Document.Project.Solution, context.CancellationToken);
return references.Any(r => r.Locations.Any());
}
}
10 changes: 7 additions & 3 deletions src/Microsoft.Unity.Analyzers/MessageSignature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,18 @@ public class MessageSignatureCodeFix : CodeFixProvider

public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
{
var methodDeclaration = await context.GetFixableNodeAsync<MethodDeclarationSyntax>();
if (methodDeclaration == null)
var method = await context.GetFixableNodeAsync<MethodDeclarationSyntax>();
if (method == null)
return;

// Do not provide a code fix if the message is -wrongly- referenced elsewhere
if (await context.IsReferencedAsync(method))
return;

context.RegisterCodeFix(
CodeAction.Create(
Strings.MessageSignatureCodeFixTitle,
ct => FixMethodDeclarationSignatureAsync(context.Document, methodDeclaration, ct),
ct => FixMethodDeclarationSignatureAsync(context.Document, method, ct),
FixableDiagnosticIds.Single()), // using DiagnosticId as equivalence key for BatchFixer
context.Diagnostics);
}
Expand Down