Fix HarmonyMethodRefParametersAnalyzer incorrectly marking member accesses as warning#6
Fix HarmonyMethodRefParametersAnalyzer incorrectly marking member accesses as warning#6xiaoxiao921 wants to merge 2 commits intoBepInEx:mainfrom
Conversation
|
Bump |
|
You link has expired. FYI "githubusercontent" links are temporary. I found this issue/PR because I was looking into calls like |
|
I don't have the screenshot anymore, wasn't aware GitHub self hosted files would expire on their own website. |
|
The problem is you reused a link from elsewhere. Should have pasted the image directly, I guess. Regardless, it wouldn't be usable when reading git history. Moreover, if it fixes a warning, you should just copy the warning text; it doesn't require a png (: |
|
Here is a minimal reproducible example, in a text format, searchable, indexable, does not expire, doesn't have anything to do with any Discord bots. using HarmonyLib;
class OriginalCode
{
public void Test() {}
}
[HarmonyPatch(typeof(OriginalCode))]
static class MyPatch
{
[HarmonyPostfix]
[HarmonyPatch(nameof(OriginalCode.Test))]
static void PropertyAccess((int, int) __state)
{
var _ = __state.Item1;
}
[HarmonyPostfix]
[HarmonyPatch(nameof(OriginalCode.Test))]
static void MethodCall(int __state)
{
__state.ToString();
}
}The code above produces the following diagnostics: SourceFile.cs(15,17): warning Harmony003: Harmony non-ref patch parameter __state.Item1 modified. This assignment have no effect.
SourceFile.cs(22,9): warning Harmony003: Harmony non-ref patch parameter __state.ToString modified. This assignment have no effect.So, apparently, accessing (reading) Furthermore, the warning is only issued for value types (like using HarmonyLib;
class OriginalCode
{
public void Test() {}
}
class State
{
public int data;
}
[HarmonyPatch(typeof(OriginalCode))]
static class MyPatch
{
[HarmonyPostfix]
[HarmonyPatch(nameof(OriginalCode.Test))]
static void CustomClass(State __state)
{
// no warning
var _ = __state.data;
__state.data = 42;
}
}Finally, the warning can be mitigated by copying a value typed state or another argument to a local variable. [HarmonyPostfix]
static void CustomClass((int, int) __state)
{
// no warning
var alias = __state;
var _ = alias.Item1;
}It's worth nothing that the official Harmony docs are clearly biased towards reference type states, so the warning does not affect the official Harmony tutorial on prefix patching. Overall, I think the warning makes sense to prevent silly mistakes, but an argument named |
|
The PR was not even about the parameter being a |
|
Yea, I was trying to analyze your solution, and conduct my own investigation as well. It was a fun ride! Also, you are right: the analyzer isn't flagging a |

Fix incorrect warnings like these