mirror of
https://github.com/godotengine/godot.git
synced 2026-01-05 06:11:29 +03:00
Merge pull request #84043 from dalexeev/gds-fix-unsafe-cast-warning
GDScript: Fix `UNSAFE_CAST` warning
This commit is contained in:
@@ -0,0 +1,4 @@
|
||||
func test():
|
||||
var node := Node.new()
|
||||
node.free()
|
||||
print(node as Node2D)
|
||||
@@ -0,0 +1,6 @@
|
||||
GDTEST_RUNTIME_ERROR
|
||||
>> SCRIPT ERROR
|
||||
>> on function: test()
|
||||
>> runtime/errors/cast_freed_object.gd
|
||||
>> 4
|
||||
>> Trying to cast a freed object.
|
||||
@@ -0,0 +1,4 @@
|
||||
func test():
|
||||
var integer: Variant = 1
|
||||
@warning_ignore("unsafe_cast")
|
||||
print(integer as Array)
|
||||
@@ -0,0 +1,6 @@
|
||||
GDTEST_RUNTIME_ERROR
|
||||
>> SCRIPT ERROR
|
||||
>> on function: test()
|
||||
>> runtime/errors/cast_int_to_array.gd
|
||||
>> 4
|
||||
>> Invalid cast: could not convert value to 'Array'.
|
||||
@@ -0,0 +1,4 @@
|
||||
func test():
|
||||
var integer: Variant = 1
|
||||
@warning_ignore("unsafe_cast")
|
||||
print(integer as Node)
|
||||
@@ -0,0 +1,6 @@
|
||||
GDTEST_RUNTIME_ERROR
|
||||
>> SCRIPT ERROR
|
||||
>> on function: test()
|
||||
>> runtime/errors/cast_int_to_object.gd
|
||||
>> 4
|
||||
>> Invalid cast: can't convert a non-object value to an object type.
|
||||
@@ -0,0 +1,4 @@
|
||||
func test():
|
||||
var object: Variant = RefCounted.new()
|
||||
@warning_ignore("unsafe_cast")
|
||||
print(object as int)
|
||||
@@ -0,0 +1,6 @@
|
||||
GDTEST_RUNTIME_ERROR
|
||||
>> SCRIPT ERROR
|
||||
>> on function: test()
|
||||
>> runtime/errors/cast_object_to_int.gd
|
||||
>> 4
|
||||
>> Invalid cast: could not convert value to 'int'.
|
||||
@@ -0,0 +1,24 @@
|
||||
func print_value(value: Variant) -> void:
|
||||
if value is Object:
|
||||
@warning_ignore("unsafe_method_access")
|
||||
print("<%s>" % value.get_class())
|
||||
else:
|
||||
print(var_to_str(value))
|
||||
|
||||
func test():
|
||||
var int_value := 1
|
||||
print_value(int_value as Variant)
|
||||
print_value(int_value as int)
|
||||
print_value(int_value as float)
|
||||
|
||||
var node_value := Node.new()
|
||||
print_value(node_value as Variant)
|
||||
print_value(node_value as Object)
|
||||
print_value(node_value as Node)
|
||||
print_value(node_value as Node2D)
|
||||
node_value.free()
|
||||
|
||||
var null_value = null
|
||||
print_value(null_value as Variant)
|
||||
@warning_ignore("unsafe_cast")
|
||||
print_value(null_value as Node)
|
||||
@@ -0,0 +1,10 @@
|
||||
GDTEST_OK
|
||||
1
|
||||
1
|
||||
1.0
|
||||
<Node>
|
||||
<Node>
|
||||
<Node>
|
||||
null
|
||||
null
|
||||
null
|
||||
Reference in New Issue
Block a user