GDScript: Warn when enum variable has no default

The default will always be set to `0`, so if it's not a valid value in
the enum, the warning is shown.
This commit is contained in:
George Marques
2024-04-16 11:16:36 -03:00
parent 658e97c93a
commit f9048fcd7d
7 changed files with 38 additions and 0 deletions

View File

@@ -0,0 +1,9 @@
enum HasZero { A = 0, B = 1 }
enum HasNoZero { A = 1, B = 2 }
var has_zero: HasZero # No warning, because the default `0` is valid.
var has_no_zero: HasNoZero # Warning, because there is no `0` in the enum.
func test():
print(has_zero)
print(has_no_zero)

View File

@@ -0,0 +1,7 @@
GDTEST_OK
>> WARNING
>> Line: 4
>> ENUM_VARIABLE_WITHOUT_DEFAULT
>> The variable "has_no_zero" has an enum type and does not set an explicit default value. The default will be set to "0".
0
0

View File

@@ -23,6 +23,7 @@ var test_var_hard_int: int
var test_var_hard_variant_type: Variant.Type
@export var test_var_hard_variant_type_exported: Variant.Type
var test_var_hard_node_process_mode: Node.ProcessMode
@warning_ignore("enum_variable_without_default")
var test_var_hard_my_enum: MyEnum
var test_var_hard_array: Array
var test_var_hard_array_int: Array[int]