summaryrefslogtreecommitdiffstats
path: root/modules/gdscript/tests/scripts/parser
diff options
context:
space:
mode:
authorGeorge Marques <george@gmarqu.es>2024-04-09 14:15:51 -0300
committerGeorge Marques <george@gmarqu.es>2024-04-10 11:59:57 -0300
commit877802e2520e03593d2e5cf76cfa7659899b1aa4 (patch)
treefd39354ece46ff416f468babd8e34d5c6f61bdd7 /modules/gdscript/tests/scripts/parser
parenta7b860250f305f6cbaf61c30f232ff3bbdfdda0b (diff)
downloadredot-engine-877802e2520e03593d2e5cf76cfa7659899b1aa4.tar.gz
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.
Diffstat (limited to 'modules/gdscript/tests/scripts/parser')
-rw-r--r--modules/gdscript/tests/scripts/parser/warnings/unassigned_variable.gd11
-rw-r--r--modules/gdscript/tests/scripts/parser/warnings/unassigned_variable.out15
2 files changed, 23 insertions, 3 deletions
diff --git a/modules/gdscript/tests/scripts/parser/warnings/unassigned_variable.gd b/modules/gdscript/tests/scripts/parser/warnings/unassigned_variable.gd
index afb5059eea..b38cffb754 100644
--- a/modules/gdscript/tests/scripts/parser/warnings/unassigned_variable.gd
+++ b/modules/gdscript/tests/scripts/parser/warnings/unassigned_variable.gd
@@ -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.
diff --git a/modules/gdscript/tests/scripts/parser/warnings/unassigned_variable.out b/modules/gdscript/tests/scripts/parser/warnings/unassigned_variable.out
index 10f89be132..36db304ef4 100644
--- a/modules/gdscript/tests/scripts/parser/warnings/unassigned_variable.out
+++ b/modules/gdscript/tests/scripts/parser/warnings/unassigned_variable.out
@@ -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>