Godot Engine – Multi-platform 2D and 3D game engine
Add warning for cast anti-pattern in `@onready` vars
HolonProductiona38846d385c4
diff --git a/doc/classes/ProjectSettings.xml b/doc/classes/ProjectSettings.xmlindex 67b2d2bc68a..1ca63a66b73 100644--- a/doc/classes/ProjectSettings.xml+++ b/doc/classes/ProjectSettings.xml@@ -617,6 +617,9 @@<member name="debug/gdscript/warnings/native_method_override" type="int" setter="" getter="" default="2">When set to [b]Warn[/b] or [b]Error[/b], produces a warning or an error respectively when a method in the script overrides a native method, because it may not behave as expected.</member>+ <member name="debug/gdscript/warnings/onready_with_cast" type="int" setter="" getter="" default="1">+ When set to [b]Warn[/b] or [b]Error[/b], produces a warning or an error respectively when an [code]@onready[/code] initializer uses a cast on a [code]$GetNodeLiteral[/code], because it may unexpectedly assign [code]null[/code].+ </member><member name="debug/gdscript/warnings/onready_with_export" type="int" setter="" getter="" default="2">When set to [b]Warn[/b] or [b]Error[/b], produces a warning or an error respectively when the [code]@onready[/code] annotation is used together with the [code]@export[/code] annotation, since it may not behave as expected.</member>diff --git a/modules/gdscript/gdscript_linter.cpp b/modules/gdscript/gdscript_linter.cppindex 3102f9673fb..73bb8400369 100644--- a/modules/gdscript/gdscript_linter.cpp+++ b/modules/gdscript/gdscript_linter.cpp@@ -68,6 +68,22 @@ public:}};+struct OnreadyWithCast final {+ static void check_variable(const GDScriptParser::VariableNode *p_variable, GDScriptParser &p_sink) {+ if (!p_variable->onready || !p_variable->initializer || p_variable->initializer->type != GDScriptParser::Node::CAST) {+ return;+ }++ const GDScriptParser::CastNode *cast = static_cast<const GDScriptParser::CastNode *>(p_variable->initializer);+ if (!cast->operand || cast->operand->type != GDScriptParser::Node::GET_NODE) {+ return;+ }++ const GDScriptParser::GetNodeNode *get_node = static_cast<const GDScriptParser::GetNodeNode *>(cast->operand);+ p_sink.push_warning(cast, GDScriptWarning::ONREADY_WITH_CAST, (get_node->use_dollar ? "$" : "%") + get_node->full_path);+ }+};+// Infrastructure ===================================================================template <typename T>@@ -88,6 +104,8 @@ constexpr GDScriptLinter::CallbackWithValidation *const GDScriptLinter::checks[]LINTER_CHECK(ConfusableIdentifier::check_function),LINTER_CHECK(ConfusableIdentifier::check_pattern),LINTER_CHECK(ConfusableIdentifier::check_signal),++ LINTER_CHECK(OnreadyWithCast::check_variable),};#undef LINTER_CHECKdiff --git a/modules/gdscript/gdscript_warning.cpp b/modules/gdscript/gdscript_warning.cppindex 9270b8f09f3..cae401e5425 100644--- a/modules/gdscript/gdscript_warning.cpp+++ b/modules/gdscript/gdscript_warning.cpp@@ -171,6 +171,8 @@ String GDScriptWarning::get_message() const {return vformat(R"*(The default value uses "%s" which won't return nodes in the scene tree before "_ready()" is called. Use the "@onready" annotation to solve this.)*", symbols[0]);case ONREADY_WITH_EXPORT:return R"("@onready" will set the default value after "@export" takes effect and will override it.)";+ case ONREADY_WITH_CAST:+ return vformat(R"("as" will silently return "null" if the type of "%s" is wrong. Prefer assigning the node directly to an explicitly typed variable to get an error in such cases.)", symbols[0]);#ifndef DISABLE_DEPRECATED// Never produced. These warnings migrated from 3.x by mistake.case PROPERTY_USED_AS_FUNCTION: // There is already an error.@@ -249,6 +251,7 @@ String GDScriptWarning::get_name_from_code(Code p_code) {PNAME("NATIVE_METHOD_OVERRIDE"),PNAME("GET_NODE_DEFAULT_WITHOUT_ONREADY"),PNAME("ONREADY_WITH_EXPORT"),+ PNAME("ONREADY_WITH_CAST"),#ifndef DISABLE_DEPRECATED"PROPERTY_USED_AS_FUNCTION","CONSTANT_USED_AS_FUNCTION",diff --git a/modules/gdscript/gdscript_warning.h b/modules/gdscript/gdscript_warning.hindex be2d891c884..c1a0f61bae1 100644--- a/modules/gdscript/gdscript_warning.h+++ b/modules/gdscript/gdscript_warning.h@@ -92,6 +92,7 @@ public:NATIVE_METHOD_OVERRIDE, // The script method overrides a native one, this may not work as intended.GET_NODE_DEFAULT_WITHOUT_ONREADY, // A class variable uses `get_node()` (or the `$` notation) as its default value, but does not use the @onready annotation.ONREADY_WITH_EXPORT, // The `@onready` annotation will set the value after `@export` which is likely not intended.+ ONREADY_WITH_CAST, // The cast will silently assign `null` if the node has a wrong type. This is likely not intended.#ifndef DISABLE_DEPRECATEDPROPERTY_USED_AS_FUNCTION, // Function not found, but there's a property with the same name.CONSTANT_USED_AS_FUNCTION, // Function not found, but there's a constant with the same name.@@ -151,6 +152,7 @@ public:ERROR, // NATIVE_METHOD_OVERRIDE // May not work as expected.ERROR, // GET_NODE_DEFAULT_WITHOUT_ONREADY // May not work as expected.ERROR, // ONREADY_WITH_EXPORT // May not work as expected.+ WARN, // ONREADY_WITH_CAST#ifndef DISABLE_DEPRECATEDWARN, // PROPERTY_USED_AS_FUNCTIONWARN, // CONSTANT_USED_AS_FUNCTIONdiff --git a/modules/gdscript/tests/scripts/analyzer/features/allow_get_node_with_onready.gd b/modules/gdscript/tests/scripts/analyzer/features/allow_get_node_with_onready.gdindex 3e647407cd3..f209e82f88d 100644--- a/modules/gdscript/tests/scripts/analyzer/features/allow_get_node_with_onready.gd+++ b/modules/gdscript/tests/scripts/analyzer/features/allow_get_node_with_onready.gd@@ -2,6 +2,7 @@ extends Node@onready var shorthand = $Node@onready var call_no_cast = get_node(^"Node")+@warning_ignore("ONREADY_WITH_CAST")@onready var shorthand_with_cast = $Node as Node@onready var call_with_cast = get_node(^"Node") as Nodediff --git a/modules/gdscript/tests/scripts/analyzer/warnings/onready_with_cast.gd b/modules/gdscript/tests/scripts/analyzer/warnings/onready_with_cast.gdnew file mode 100644index 00000000000..a6c3702d163--- /dev/null+++ b/modules/gdscript/tests/scripts/analyzer/warnings/onready_with_cast.gd@@ -0,0 +1,12 @@+extends Node++@onready var untyped_cast = $Node as Node2D+@onready var inferred_cast := $Node as Node2D+@onready var explicit_cast: Node2D = $Node as Node2D++@onready var untyped_direct = $Node+@onready var inferred_direct := $Node+@onready var explicit_direct: Node2D = $Node++func test():+ passdiff --git a/modules/gdscript/tests/scripts/analyzer/warnings/onready_with_cast.out b/modules/gdscript/tests/scripts/analyzer/warnings/onready_with_cast.outnew file mode 100644index 00000000000..51784c04805--- /dev/null+++ b/modules/gdscript/tests/scripts/analyzer/warnings/onready_with_cast.out@@ -0,0 +1,4 @@+GDTEST_OK+~~ WARNING at line 3: (ONREADY_WITH_CAST) "as" will silently return "null" if the type of "$Node" is wrong. Prefer assigning the node directly to an explicitly typed variable to get an error in such cases.+~~ WARNING at line 4: (ONREADY_WITH_CAST) "as" will silently return "null" if the type of "$Node" is wrong. Prefer assigning the node directly to an explicitly typed variable to get an error in such cases.+~~ WARNING at line 5: (ONREADY_WITH_CAST) "as" will silently return "null" if the type of "$Node" is wrong. Prefer assigning the node directly to an explicitly typed variable to get an error in such cases.