From 638148a184e9526bf3519de44e5f34791604b50b Mon Sep 17 00:00:00 2001 From: Danil Alexeev Date: Mon, 22 Jul 2024 21:45:01 +0300 Subject: [PATCH] GDScript: Fix false positive cases of `ENUM_VARIABLE_WITHOUT_DEFAULT` --- modules/gdscript/gdscript_analyzer.cpp | 4 ++-- .../warnings/enum_without_default_value.gd | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/modules/gdscript/gdscript_analyzer.cpp b/modules/gdscript/gdscript_analyzer.cpp index 67b40a6198f..bfc78d26055 100644 --- a/modules/gdscript/gdscript_analyzer.cpp +++ b/modules/gdscript/gdscript_analyzer.cpp @@ -1967,8 +1967,8 @@ void GDScriptAnalyzer::resolve_assignable(GDScriptParser::AssignableNode *p_assi } #ifdef DEBUG_ENABLED + const bool is_parameter = p_assignable->type == GDScriptParser::Node::PARAMETER; if (!has_specified_type) { - const bool is_parameter = p_assignable->type == GDScriptParser::Node::PARAMETER; const String declaration_type = is_constant ? "Constant" : (is_parameter ? "Parameter" : "Variable"); if (p_assignable->infer_datatype || is_constant) { // Do not produce the `INFERRED_DECLARATION` warning on type import because there is no way to specify the true type. @@ -1980,7 +1980,7 @@ void GDScriptAnalyzer::resolve_assignable(GDScriptParser::AssignableNode *p_assi } else { parser->push_warning(p_assignable, GDScriptWarning::UNTYPED_DECLARATION, declaration_type, p_assignable->identifier->name); } - } else if (specified_type.kind == GDScriptParser::DataType::ENUM && p_assignable->initializer == nullptr) { + } else if (!is_parameter && specified_type.kind == GDScriptParser::DataType::ENUM && p_assignable->initializer == nullptr) { // Warn about enum variables without default value. Unless the enum defines the "0" value, then it's fine. bool has_zero_value = false; for (const KeyValue &kv : specified_type.enum_values) { diff --git a/modules/gdscript/tests/scripts/analyzer/warnings/enum_without_default_value.gd b/modules/gdscript/tests/scripts/analyzer/warnings/enum_without_default_value.gd index 13e3edf93f5..f3a8661acfe 100644 --- a/modules/gdscript/tests/scripts/analyzer/warnings/enum_without_default_value.gd +++ b/modules/gdscript/tests/scripts/analyzer/warnings/enum_without_default_value.gd @@ -7,3 +7,17 @@ var has_no_zero: HasNoZero # Warning, because there is no `0` in the enum. func test(): print(has_zero) print(has_no_zero) + + +# GH-94634. A parameter is either mandatory or has a default value. +func test_no_exec(param: HasNoZero) -> void: + print(param) + + # Loop iterator always has a value. + for i: HasNoZero in HasNoZero.values(): + print(i) + + match param: + # Pattern bind always has a value. + var x: + print(x)