Skip to content
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
5 changes: 5 additions & 0 deletions Harmonize.Analyzers/Analyzers/UnspecifiedPatchTypeAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ private void AnalyzeMethod(SyntaxNodeAnalysisContext context)
return;
}

if (AuxilaryMethodClassifier.IsAnyAuxilaryMethodCandidate(method))
{
return;
}

// see if this method is used as a helper from with the patch class.
// usage from outside the patch class cannot be detected because SymbolFinder
// requires the workspace API which is not available to CLI builds.
Expand Down
94 changes: 94 additions & 0 deletions Harmonize.Analyzers/AuxilaryMethodClassifier.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
using Microsoft.CodeAnalysis;

namespace Harmonize;

internal static class AuxilaryMethodClassifier
{
public static bool IsPrepareCandidate(IMethodSymbol method)
{
if (method.Name == "Prepare")
{
return true;
}

foreach (AttributeData attr in method.GetAttributes())
{
if (
attr.AttributeClass?.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat)
== "global::HarmonyLib.HarmonyPrepare"
)
{
return true;
}
}
return false;
}

public static bool IsTargetMethodCandidate(IMethodSymbol method)
{
if (method.Name == "TargetMethod")
{
return true;
}

foreach (AttributeData attr in method.GetAttributes())
{
if (
attr.AttributeClass?.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat)
== "global::HarmonyLib.HarmonyTargetMethod"
)
{
return true;
}
}
return false;
}

public static bool IsTargetMethodsCandidate(IMethodSymbol method)
{
if (method.Name == "TargetMethods")
{
return true;
}

foreach (AttributeData attr in method.GetAttributes())
{
if (
attr.AttributeClass?.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat)
== "global::HarmonyLib.HarmonyTargetMethods"
)
{
return true;
}
}
return false;
}

public static bool IsCleanupCandidate(IMethodSymbol method)
{
if (method.Name == "Cleanup")
{
return true;
}

foreach (AttributeData attr in method.GetAttributes())
{
if (
attr.AttributeClass?.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat)
== "global::HarmonyLib.HarmonyCleanup"
)
{
return true;
}
}
return false;
}

public static bool IsAnyAuxilaryMethodCandidate(IMethodSymbol method)
{
return IsPrepareCandidate(method)
|| IsTargetMethodCandidate(method)
|| IsTargetMethodsCandidate(method)
|| IsCleanupCandidate(method);
}
}
2 changes: 1 addition & 1 deletion Harmonize.Package/Harmonize.Package.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<PropertyGroup>
<PackageId>Harmonize</PackageId>
<PackageVersion>1.0.5</PackageVersion>
<PackageVersion>1.0.6</PackageVersion>
<Authors>BadMagic100</Authors>
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
Expand Down
41 changes: 41 additions & 0 deletions Harmonize.Test/Fixes/TestUnspecifiedPatchTypeFixer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,47 @@ private static void Helper() { }
await VerifyCS.VerifyAnalyzerOnlyAsync(source, [], TestContext.Current.CancellationToken);
}

[Theory(DisplayName = "should not emit diagnostic for auxilary methods by name")]
[InlineData(["Prepare"])]
[InlineData(["TargetMethod"])]
[InlineData(["TargetMethods"])]
[InlineData(["Cleanup"])]
public async Task TestAuxilaryMethodNoDiagnosticByName(string name)
{
string source = /*lang=c#-test*/
$$"""
using HarmonyLib;

[HarmonyPatch(typeof(string), nameof(string.Copy))]
class Patches
{
private static void {{name}}() { }
}
""";
await VerifyCS.VerifyAnalyzerOnlyAsync(source, [], TestContext.Current.CancellationToken);
}

[Theory(DisplayName = "should not emit diagnostic for auxilary methods by attribute")]
[InlineData(["Prepare"])]
[InlineData(["TargetMethod"])]
[InlineData(["TargetMethods"])]
[InlineData(["Cleanup"])]
public async Task TestAuxilaryMethodNoDiagnosticByAttribute(string attr)
{
string source = /*lang=c#-test*/
$$"""
using HarmonyLib;

[HarmonyPatch(typeof(string), nameof(string.Copy))]
class Patches
{
[Harmony{{attr}}]
private static void Foo() { }
}
""";
await VerifyCS.VerifyAnalyzerOnlyAsync(source, [], TestContext.Current.CancellationToken);
}

[Fact(DisplayName = "should provide diagnostic without fix for multiple patch types")]
public async Task TestMultiplePatchTypes()
{
Expand Down
Loading