mirror of
https://github.com/godotengine/godot.git
synced 2025-11-08 17:41:19 +00:00
Merge pull request #70656 from vonagam/fix-void-returns
GDScript: Disallow return with value in void functions
This commit is contained in:
commit
e4c1103af4
7 changed files with 20 additions and 0 deletions
|
|
@ -2047,6 +2047,9 @@ void GDScriptAnalyzer::resolve_return(GDScriptParser::ReturnNode *p_return) {
|
||||||
update_array_literal_element_type(expected_type, static_cast<GDScriptParser::ArrayNode *>(p_return->return_value));
|
update_array_literal_element_type(expected_type, static_cast<GDScriptParser::ArrayNode *>(p_return->return_value));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (has_expected_type && expected_type.is_hard_type() && expected_type.kind == GDScriptParser::DataType::BUILTIN && expected_type.builtin_type == Variant::NIL) {
|
||||||
|
push_error("A void function cannot return a value.", p_return);
|
||||||
|
}
|
||||||
result = p_return->return_value->get_datatype();
|
result = p_return->return_value->get_datatype();
|
||||||
} else {
|
} else {
|
||||||
// Return type is null by default.
|
// Return type is null by default.
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,2 @@
|
||||||
|
func test() -> void:
|
||||||
|
return null
|
||||||
|
|
@ -0,0 +1,2 @@
|
||||||
|
GDTEST_ANALYZER_ERROR
|
||||||
|
A void function cannot return a value.
|
||||||
|
|
@ -0,0 +1,4 @@
|
||||||
|
func test() -> void:
|
||||||
|
var a
|
||||||
|
a = 1
|
||||||
|
return a
|
||||||
|
|
@ -0,0 +1,2 @@
|
||||||
|
GDTEST_ANALYZER_ERROR
|
||||||
|
A void function cannot return a value.
|
||||||
|
|
@ -0,0 +1,5 @@
|
||||||
|
func variant() -> Variant:
|
||||||
|
return 'variant'
|
||||||
|
|
||||||
|
func test():
|
||||||
|
print(variant())
|
||||||
|
|
@ -0,0 +1,2 @@
|
||||||
|
GDTEST_OK
|
||||||
|
variant
|
||||||
Loading…
Add table
Add a link
Reference in a new issue