Skip to content

Fix create_script validator false-positive on constructor invocations#1045

Merged
dsarno merged 2 commits intoCoplayDev:betafrom
dsarno:fix/constructor-invocation-false-positive
Apr 7, 2026
Merged

Fix create_script validator false-positive on constructor invocations#1045
dsarno merged 2 commits intoCoplayDev:betafrom
dsarno:fix/constructor-invocation-false-positive

Conversation

@dsarno
Copy link
Copy Markdown
Collaborator

@dsarno dsarno commented Apr 7, 2026

Summary

Test plan

  • DuplicateMethodCheck_ConstructorInvocations_NotFlagged — verifies new GameObject("A"); new GameObject("B"); no longer triggers false positive
  • DuplicateMethodCheck_MultipleDistinctConstructors_NotFlagged — verifies mixed new MaterialPropertyBlock() + new GameObject() calls pass
  • Full EditMode test suite (902 tests): 838 passed, 0 failed, 46 skipped, 18 inconclusive

🤖 Generated with Claude Code

Summary by Sourcery

Prevent the script validation duplicate-method check from misidentifying constructor invocations as duplicate method declarations.

Bug Fixes:

  • Exclude constructor invocations from the duplicate method signature regex by detecting and skipping matches where the captured return token is 'new'.

Tests:

  • Add regression tests ensuring constructor calls, including multiple constructors of different types, are not flagged as duplicate methods.

Summary by CodeRabbit

  • Bug Fixes

    • Improved duplicate-method detection to avoid false positives when constructor invocations using the new keyword appear, so unrelated new usages no longer trigger duplicate-method errors.
  • Tests

    • Added new unit tests covering scenarios with multiple new constructor calls and derived-class new modifiers to validate the fix.

The regex-based duplicate method signature check misidentified
`new Type(...)` constructor calls as method declarations, causing
valid C# like `new GameObject("A"); new GameObject("B");` to be
rejected. Capture the return-type token and skip when it is `new`.

Addresses CoplayDev#1044

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@sourcery-ai
Copy link
Copy Markdown
Contributor

sourcery-ai bot commented Apr 7, 2026

Reviewer's Guide

Adjusts the ManageScript method-signature regex and handling to ignore constructor invocations misclassified as method declarations, and adds focused unit tests to prevent regressions in the duplicate-method validator.

Class diagram for ManageScript duplicate method signature check and related tests

classDiagram
    class ManageScript {
        +CheckDuplicateMethodSignatures(string contents, System.Collections.Generic.IList~string~ errors)
        -IsCSharpKeyword(string identifier) bool
        -CountTopLevelParams(string paramList) int
        -ExtractParamTypes(string paramList) string
    }

    class ManageScriptValidationTests {
        +DuplicateMethodCheck_ConstructorInvocations_NotFlagged()
        +DuplicateMethodCheck_MultipleDistinctConstructors_NotFlagged()
    }

    ManageScriptValidationTests ..> ManageScript : uses
Loading

File-Level Changes

Change Details Files
Refine duplicate-method signature detection to correctly parse return type and ignore constructor invocations.
  • Updated the method signature regex to capture return type, method name, and parameter list in separate groups instead of only the method name and parameters.
  • Introduced a returnType variable extracted from the regex match and shifted methodName and parameter group indices accordingly.
  • Skipped matches where the captured return type is the keyword new, treating them as constructor invocations rather than method declarations.
  • Adjusted parameter counting and type extraction calls to use the new parameter group index when building the duplicate-signature key.
MCPForUnity/Editor/Tools/ManageScript.cs
Add regression tests ensuring constructor invocations are not flagged as duplicate methods.
  • Added a test that validates two new GameObject(...) calls in a Start method do not trigger duplicate-method errors.
  • Added a test that validates multiple constructor calls for MaterialPropertyBlock and GameObject of different types are not misclassified as duplicate methods.
TestProjects/UnityMCPTests/Assets/Tests/EditMode/Tools/ManageScriptValidationTests.cs

Possibly linked issues


Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Apr 7, 2026

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: db6a213a-5c06-439f-8420-20565252139d

📥 Commits

Reviewing files that changed from the base of the PR and between b364834 and ed5323a.

📒 Files selected for processing (2)
  • MCPForUnity/Editor/Tools/ManageScript.cs
  • TestProjects/UnityMCPTests/Assets/Tests/EditMode/Tools/ManageScriptValidationTests.cs

📝 Walkthrough

Walkthrough

Adjusted duplicate-method-signature detection to correctly separate captured return type from method name in the regex, skip matches where the captured return type is "new", and fix parameter-group extraction. Added NUnit tests ensuring new Type(...) constructor invocations are not reported as duplicate methods.

Changes

Cohort / File(s) Summary
Duplicate Method Detection Logic
MCPForUnity/Editor/Tools/ManageScript.cs
Regex now captures return type (group 1) and method name (group 2) separately; skips matches where returnType == "new"; parameter counting/extraction uses the corrected group index.
Constructor Invocation Test Coverage
TestProjects/UnityMCPTests/Assets/Tests/EditMode/Tools/ManageScriptValidationTests.cs
Added three NUnit tests verifying that constructor invocations (new Type(...)) are not misclassified as duplicate method signatures, including cases with multiple identical and different new calls and a new-modifier method in a derived class.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related issues

Possibly related PRs

Poem

🐰 I hopped through code with careful nose,

Skipped the new where constructors pose,
Methods now counted, names set apart,
No false duplicates to break the heart —
A tidy patch from rabbit art.

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 40.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately and concisely describes the primary fix: resolving false-positive duplicate-method detection triggered by constructor invocations.
Description check ✅ Passed The description covers all required template sections: summary with issue reference, type of change (bug fix), changes made, comprehensive testing details, and related issues. Documentation updates section is marked as not applicable.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've left some high level feedback:

  • When checking returnType == "new", consider using string.Equals(returnType, "new", StringComparison.Ordinal) to make the intent explicit and avoid any surprises from culture-sensitive comparisons in the future.
  • Since the regex now relies on detecting new as the captured return type to distinguish constructor calls, it may be worth adding one more test that includes a method with the new modifier plus nearby constructor invocations to guard against regressions in how modifiers vs. return types are parsed.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- When checking `returnType == "new"`, consider using `string.Equals(returnType, "new", StringComparison.Ordinal)` to make the intent explicit and avoid any surprises from culture-sensitive comparisons in the future.
- Since the regex now relies on detecting `new` as the captured return type to distinguish constructor calls, it may be worth adding one more test that includes a method with the `new` modifier plus nearby constructor invocations to guard against regressions in how modifiers vs. return types are parsed.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Use string.Equals with StringComparison.Ordinal for the "new" check.
Add test combining `public new void Init()` with nearby constructor
invocations to guard against modifier vs return-type parsing regressions.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@dsarno dsarno merged commit 7f5cd16 into CoplayDev:beta Apr 7, 2026
1 check was pending
@dsarno dsarno deleted the fix/constructor-invocation-false-positive branch April 7, 2026 03:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant