Skip to content

Commit 74ad91c

Browse files
authored
Merge pull request #1076 from justinpbarnett/fix/manage-script-local-function-duplicates
Fix create_script validator false-positive inside method bodies
2 parents 6577c29 + 0c76786 commit 74ad91c

2 files changed

Lines changed: 54 additions & 1 deletion

File tree

MCPForUnity/Editor/Tools/ManageScript.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2714,12 +2714,18 @@ private static void CheckDuplicateMethodSignatures(string contents, System.Colle
27142714
}
27152715
// Second pass: build per-position containing type array
27162716
var containingTypeArr = new string[codeOnly.Length];
2717+
var braceDepthArr = new int[codeOnly.Length];
2718+
var typeMemberDepthArr = new int[codeOnly.Length];
27172719
{
27182720
var stack = new System.Collections.Generic.List<(string name, int openDepth)>();
27192721
int bd2 = 0;
27202722
string current = "";
27212723
for (int i = 0; i < codeOnly.Length; i++)
27222724
{
2725+
containingTypeArr[i] = current;
2726+
braceDepthArr[i] = bd2;
2727+
typeMemberDepthArr[i] = stack.Count > 0 ? stack[stack.Count - 1].openDepth + 1 : -1;
2728+
27232729
if (codeOnly[i] == '{')
27242730
{
27252731
if (typeBraceMap.TryGetValue(i, out string tn))
@@ -2738,7 +2744,6 @@ private static void CheckDuplicateMethodSignatures(string contents, System.Colle
27382744
current = stack.Count > 0 ? stack[stack.Count - 1].name : "";
27392745
}
27402746
}
2741-
containingTypeArr[i] = current;
27422747
}
27432748
}
27442749

@@ -2757,6 +2762,8 @@ private static void CheckDuplicateMethodSignatures(string contents, System.Colle
27572762
int paramCount = CountTopLevelParams(sm.Groups[3].Value);
27582763
string paramTypes = ExtractParamTypes(sm.Groups[3].Value);
27592764
string containingType = containingTypeArr[sm.Index];
2765+
if (string.IsNullOrEmpty(containingType)) continue;
2766+
if (braceDepthArr[sm.Index] != typeMemberDepthArr[sm.Index]) continue;
27602767
string key = $"{containingType}/{methodName}/{paramCount}/{paramTypes}";
27612768
if (seen.TryGetValue(key, out _))
27622769
errors.Add($"ERROR: Duplicate method signature detected: '{methodName}' with {paramCount} parameter(s). This may indicate a corrupted edit.");

TestProjects/UnityMCPTests/Assets/Tests/EditMode/Tools/ManageScriptValidationTests.cs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,52 @@ void Start()
378378
"new modifier on method should not interfere with constructor invocation filtering");
379379
}
380380

381+
[Test]
382+
public void DuplicateMethodCheck_LocalFunctionsInDifferentMethods_NotFlagged()
383+
{
384+
string code = @"using UnityEngine;
385+
public class Test : MonoBehaviour
386+
{
387+
void Start()
388+
{
389+
int ClampScore(int value) { return Mathf.Clamp(value, 0, 100); }
390+
Debug.Log(ClampScore(10));
391+
}
392+
393+
void Reset()
394+
{
395+
int ClampScore(int value) { return Mathf.Clamp(value, 0, 100); }
396+
Debug.Log(ClampScore(0));
397+
}
398+
}";
399+
var errors = CallValidateScriptSyntaxUnity(code);
400+
Assert.IsFalse(HasDuplicateMethodError(errors),
401+
"Same-signature local functions in different methods are valid C# and should not be flagged as duplicate class methods");
402+
}
403+
404+
[Test]
405+
public void DuplicateMethodCheck_RepeatedMethodInvocations_NotFlagged()
406+
{
407+
string code = @"using UnityEngine;
408+
public class Test : MonoBehaviour
409+
{
410+
int First()
411+
{
412+
return Compute();
413+
}
414+
415+
int Second()
416+
{
417+
return Compute();
418+
}
419+
420+
int Compute() { return 1; }
421+
}";
422+
var errors = CallValidateScriptSyntaxUnity(code);
423+
Assert.IsFalse(HasDuplicateMethodError(errors),
424+
"Repeated method invocations inside method bodies should not be treated as duplicate method declarations");
425+
}
426+
381427
[Test]
382428
public void HandleCommand_PathWithCsExtension_StripsFilename()
383429
{

0 commit comments

Comments
 (0)