Microsoft.Unity.Analyzers/doc/USP0011.md

855 B

USP0011 Don't make fields with the ContextMenuItem attribute read-only

Fields decorated with the ContextMenuItem attribute should not be made read-only.

Suppressed Diagnostic ID

IDE0044 - Make field readonly

Examples of code that produces a suppressed diagnostic

using UnityEngine;

class Camera : MonoBehaviour
{
    [ContextMenuItem("Foo", "Bar")]
    private string contextMenuString = "";
}

Why is the diagnostic reported?

The IDE does not detect that contextMenuString is ever assigned outside of the declaration or in a constructor. Therefore, under normal circumstances, it would be reasonable to mark the field as read-only.

Why do we suppress this diagnostic?

A field with the ContextMenuItem attribute is used indirectly by Unity. Making the field read-only would prevent it from being changed by Unity.