Skip to content

UNT0039: do not report GetComponent inside editor-only messages#478

Merged
sailro merged 1 commit into
microsoft:mainfrom
Theorist100:unt0039-skip-editor-only-messages
Jul 17, 2026
Merged

UNT0039: do not report GetComponent inside editor-only messages#478
sailro merged 1 commit into
microsoft:mainfrom
Theorist100:unt0039-skip-editor-only-messages

Conversation

@Theorist100

Copy link
Copy Markdown
Contributor

Fixes #453

Why

UNT0039 suggests [RequireComponent] whenever a MonoBehaviour self-invokes GetComponent<T>(). A common editor pattern is to populate a serialized field with a default reference inside Reset(), letting users override it in the inspector. In that context the component is not a hard requirement, so suggesting [RequireComponent] is misleading.

What

  • UNT0039 now skips GetComponent invocations made inside editor-only Unity messages (Reset, OnValidate).
  • The editor-only message detection previously private to GameObjectIsStaticAnalyzer (UNT0040) is moved to SyntaxNodeExtensions and shared by both analyzers; no behavior change for UNT0040.
  • Tests: no diagnostic in Reset / OnValidate; a Reset(int) overload (not a Unity message) still reports. Doc page updated with a non-flagged example.

Notes

The issue mentions Reset; OnValidate is included because it is the same editor-only prefill context and the existing editor-only message pair already models it. Happy to narrow this to Reset only if preferred.

GetComponent calls in Reset/OnValidate are typically used to populate
serialized fields with default references that a user can override, so
the component is not a hard requirement (microsoft#453).

Reuse the editor-only message detection already used by UNT0040 by
moving it to SyntaxNodeExtensions.
@Theorist100
Theorist100 requested a review from a team as a code owner July 8, 2026 13:36
@Theorist100

Copy link
Copy Markdown
Contributor Author

@microsoft-github-policy-service agree

@sailro
sailro merged commit 1b72c73 into microsoft:main Jul 17, 2026
6 checks passed
@sailro

sailro commented Jul 17, 2026

Copy link
Copy Markdown
Member

Thank you for this contribution !

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.

Disable UNT0039 in Reset methods

2 participants