GDScript: Don't warn on unassigned for builtin-typed variables

If the type of a variable is a built-in Variant type, then it will
automatically be assigned a default value based on the type. This means
that the explicit initialization may be unnecessary. Thus this commit
removes the warning in such case.

This also changes the meaning of the unassigned warning to happen when
the variable is used before being assigned, not when it has zero
assignments.
This commit is contained in:
George Marques
2024-04-09 14:15:51 -03:00
parent a7b860250f
commit 877802e252
10 changed files with 65 additions and 30 deletions

View File

@@ -11,6 +11,7 @@ class InnerClass:
var e2: InnerClass.MyEnum
var e3: EnumTypecheckOuterClass.InnerClass.MyEnum
@warning_ignore("unassigned_variable")
print("Self ", e1, e2, e3)
e1 = MyEnum.V1
e2 = MyEnum.V1
@@ -48,6 +49,7 @@ func test_outer_from_outer():
var e1: MyEnum
var e2: EnumTypecheckOuterClass.MyEnum
@warning_ignore("unassigned_variable")
print("Self ", e1, e2)
e1 = MyEnum.V1
e2 = MyEnum.V1
@@ -66,6 +68,7 @@ func test_inner_from_outer():
var e1: InnerClass.MyEnum
var e2: EnumTypecheckOuterClass.InnerClass.MyEnum
@warning_ignore("unassigned_variable")
print("Inner ", e1, e2)
e1 = InnerClass.MyEnum.V1
e2 = InnerClass.MyEnum.V1

View File

@@ -0,0 +1,7 @@
# GH-88117, GH-85796
func test():
var array: Array
# Should not emit unassigned warning because the Array type has a default value.
array.assign([1, 2, 3])
print(array)

View File

@@ -0,0 +1,2 @@
GDTEST_OK
[1, 2, 3]

View File

@@ -31,8 +31,8 @@ func int_func() -> int:
func test_warnings(unused_private_class_variable):
var t = 1
@warning_ignore("unassigned_variable")
var unassigned_variable
@warning_ignore("unassigned_variable")
print(unassigned_variable)
var _unassigned_variable_op_assign

View File

@@ -1,2 +1,11 @@
func test():
var __
var unassigned
print(unassigned)
unassigned = "something" # Assigned only after use.
var a
print(a) # Unassigned, warn.
if a: # Still unassigned, warn.
a = 1
print(a) # Assigned (dead code), don't warn.
print(a) # "Maybe" assigned, don't warn.

View File

@@ -1,5 +1,16 @@
GDTEST_OK
>> WARNING
>> Line: 2
>> Line: 3
>> UNASSIGNED_VARIABLE
>> The variable "__" was used but never assigned a value.
>> The variable "unassigned" was used before being assigned a value.
>> WARNING
>> Line: 7
>> UNASSIGNED_VARIABLE
>> The variable "a" was used before being assigned a value.
>> WARNING
>> Line: 8
>> UNASSIGNED_VARIABLE
>> The variable "a" was used before being assigned a value.
<null>
<null>
<null>

View File

@@ -7,6 +7,7 @@ func test():
var b
if true:
var c
@warning_ignore("unassigned_variable")
prints("Begin:", i, a, b, c)
a = 1
b = 1
@@ -20,6 +21,7 @@ func test():
var b
if true:
var c
@warning_ignore("unassigned_variable")
prints("Begin:", j, a, b, c)
a = 1
b = 1