GD0302: The generic type parameter must be annotated with the ‘[MustBeVariant]‘ attribute

Rule ID

GD0302

Category

Usage

Fix is breaking or non-breaking

Breaking

Enabled by default

Yes

Cause

A generic type is specified for a generic type argument when a Variant-compatible type is expected, but the specified generic type is not annotated with the [MustBeVariant] attribute.

Rule description

When a generic type parameter is annotated with the [MustBeVariant] attribute, the generic type is required to be a Variant-compatible type. When the type used is also a generic type, this generic type must be annotated with the [MustBeVariant] attribute as well. For example, the generic Godot.Collections.Array<T> type only supports items of a type that can be converted to Variant, a generic type can be specified if it’s properly annotated.

  1. public void Method1<T>()
  2. {
  3. // T is not valid here because it may not a Variant-compatible type.
  4. var invalidArray = new Godot.Collections.Array<T>();
  5. }
  6. public void Method2<[MustBeVariant] T>()
  7. {
  8. // T is guaranteed to be a Variant-compatible type because it's annotated
  9. // with the [MustBeVariant] attribute, so it can be used here.
  10. var validArray = new Godot.Collections.Array<T>();
  11. }

How to fix violations

To fix a violation of this rule, add the [MustBeVariant] attribute to the generic type that is used as a generic type argument that must be Variant-compatible.

When to suppress warnings

Do not suppress a warning from this rule. API that contains generic type arguments annotated with the [MustBeVariant] attribute usually has this requirement because the values will be passed to the engine, if the type can’t be marshalled it will result in runtime errors.


User-contributed notes

Please read the User-contributed notes policy before submitting a comment.

Previous Next