From 25c90836fd408566b70be8b3d79b9dbafd2eaec1 Mon Sep 17 00:00:00 2001 From: "ocean (they/them)" Date: Mon, 22 May 2023 09:21:25 -0400 Subject: [PATCH] GDScript: do not warn of return value discarded for super() inside _init() DO NOT BATCH MERGE WITH #77324, WILL RESULT IN BROKEN CI Currently, calling super() inside _init() throws a RETURN_VALUE_DISCARDED warning. The analyzer identifies super() as being a constructor, which therefore returns an object of the relevant class. However, super() isn't really a constructor by itself: in this case, it is _part_ of the constructor, and so doesn't "return" a value. A test case for this is already in #77324, which contains the warning. I am duplicating it here, without the warning, and it should conflict with the other PR. --- modules/gdscript/gdscript_analyzer.cpp | 5 +++-- .../analyzer/features/virtual_super_implemented.gd | 10 ++++++++++ .../analyzer/features/virtual_super_implemented.out | 1 + 3 files changed, 14 insertions(+), 2 deletions(-) create mode 100644 modules/gdscript/tests/scripts/analyzer/features/virtual_super_implemented.gd create mode 100644 modules/gdscript/tests/scripts/analyzer/features/virtual_super_implemented.out diff --git a/modules/gdscript/gdscript_analyzer.cpp b/modules/gdscript/gdscript_analyzer.cpp index 96bd8aafad0..cb683f3cc6c 100644 --- a/modules/gdscript/gdscript_analyzer.cpp +++ b/modules/gdscript/gdscript_analyzer.cpp @@ -3122,7 +3122,8 @@ void GDScriptAnalyzer::reduce_call(GDScriptParser::CallNode *p_call, bool p_is_a } #ifdef DEBUG_ENABLED - if (p_is_root && return_type.kind != GDScriptParser::DataType::UNRESOLVED && return_type.builtin_type != Variant::NIL) { + if (p_is_root && return_type.kind != GDScriptParser::DataType::UNRESOLVED && return_type.builtin_type != Variant::NIL && + !(p_call->is_super && p_call->function_name == GDScriptLanguage::get_singleton()->strings._init)) { parser->push_warning(p_call, GDScriptWarning::RETURN_VALUE_DISCARDED, p_call->function_name); } @@ -4710,7 +4711,7 @@ bool GDScriptAnalyzer::get_function_signature(GDScriptParser::Node *p_source, bo } if (p_is_constructor) { - function_name = "_init"; + function_name = GDScriptLanguage::get_singleton()->strings._init; r_static = true; } diff --git a/modules/gdscript/tests/scripts/analyzer/features/virtual_super_implemented.gd b/modules/gdscript/tests/scripts/analyzer/features/virtual_super_implemented.gd new file mode 100644 index 00000000000..c447003619a --- /dev/null +++ b/modules/gdscript/tests/scripts/analyzer/features/virtual_super_implemented.gd @@ -0,0 +1,10 @@ +class TestOne: + func _init(): + pass + +class TestTwo extends TestOne: + func _init(): + super() + +func test(): + pass diff --git a/modules/gdscript/tests/scripts/analyzer/features/virtual_super_implemented.out b/modules/gdscript/tests/scripts/analyzer/features/virtual_super_implemented.out new file mode 100644 index 00000000000..d73c5eb7cde --- /dev/null +++ b/modules/gdscript/tests/scripts/analyzer/features/virtual_super_implemented.out @@ -0,0 +1 @@ +GDTEST_OK