GDScript: Fix UNSAFE_CAST warning

This commit is contained in:
Danil Alexeev 2023-10-27 11:08:54 +03:00
parent 09946f79bd
commit 6e996a597f
No known key found for this signature in database
GPG key ID: 124453E157DA8DC7
22 changed files with 150 additions and 6 deletions

View file

@ -0,0 +1,3 @@
func test():
var integer := 1
print(integer as Array)

View file

@ -0,0 +1,2 @@
GDTEST_ANALYZER_ERROR
Invalid cast. Cannot convert from "int" to "Array".

View file

@ -0,0 +1,3 @@
func test():
var integer := 1
print(integer as Node)

View file

@ -0,0 +1,2 @@
GDTEST_ANALYZER_ERROR
Invalid cast. Cannot convert from "int" to "Node".

View file

@ -0,0 +1,3 @@
func test():
var object := RefCounted.new()
print(object as int)

View file

@ -0,0 +1,2 @@
GDTEST_ANALYZER_ERROR
Invalid cast. Cannot convert from "RefCounted" to "int".

View file

@ -0,0 +1,24 @@
# We don't want to execute it because of errors, just analyze.
func no_exec_test():
var weak_int = 1
print(weak_int as Variant) # No warning.
print(weak_int as int)
print(weak_int as Node)
var weak_node = Node.new()
print(weak_node as Variant) # No warning.
print(weak_node as int)
print(weak_node as Node)
var weak_variant = null
print(weak_variant as Variant) # No warning.
print(weak_variant as int)
print(weak_variant as Node)
var hard_variant: Variant = null
print(hard_variant as Variant) # No warning.
print(hard_variant as int)
print(hard_variant as Node)
func test():
pass

View file

@ -0,0 +1,33 @@
GDTEST_OK
>> WARNING
>> Line: 5
>> UNSAFE_CAST
>> Casting "Variant" to "int" is unsafe.
>> WARNING
>> Line: 6
>> UNSAFE_CAST
>> Casting "Variant" to "Node" is unsafe.
>> WARNING
>> Line: 10
>> UNSAFE_CAST
>> Casting "Variant" to "int" is unsafe.
>> WARNING
>> Line: 11
>> UNSAFE_CAST
>> Casting "Variant" to "Node" is unsafe.
>> WARNING
>> Line: 15
>> UNSAFE_CAST
>> Casting "Variant" to "int" is unsafe.
>> WARNING
>> Line: 16
>> UNSAFE_CAST
>> Casting "Variant" to "Node" is unsafe.
>> WARNING
>> Line: 20
>> UNSAFE_CAST
>> Casting "Variant" to "int" is unsafe.
>> WARNING
>> Line: 21
>> UNSAFE_CAST
>> Casting "Variant" to "Node" is unsafe.