Skip to content

Commit d3be91e

Browse files
committed
Merge pull request godotengine#89675 from dalexeev/gds-correct-unused-signal-warning
GDScript: Do not produce `UNUSED_SIGNAL` warning for common implicit uses
2 parents 88ed6af + d1e2afa commit d3be91e

File tree

3 files changed

+63
-8
lines changed

3 files changed

+63
-8
lines changed

modules/gdscript/gdscript_analyzer.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3245,6 +3245,26 @@ void GDScriptAnalyzer::reduce_call(GDScriptParser::CallNode *p_call, bool p_is_a
32453245
push_error(vformat(R"(No constructor of "%s" matches the signature "%s".)", Variant::get_type_name(builtin_type), signature), p_call);
32463246
}
32473247
}
3248+
3249+
#ifdef DEBUG_ENABLED
3250+
// Consider `Signal(self, "my_signal")` as an implicit use of the signal.
3251+
if (builtin_type == Variant::SIGNAL && p_call->arguments.size() >= 2) {
3252+
const GDScriptParser::ExpressionNode *object_arg = p_call->arguments[0];
3253+
if (object_arg && object_arg->type == GDScriptParser::Node::SELF) {
3254+
const GDScriptParser::ExpressionNode *signal_arg = p_call->arguments[1];
3255+
if (signal_arg && signal_arg->is_constant) {
3256+
const StringName &signal_name = signal_arg->reduced_value;
3257+
if (parser->current_class->has_member(signal_name)) {
3258+
const GDScriptParser::ClassNode::Member &member = parser->current_class->get_member(signal_name);
3259+
if (member.type == GDScriptParser::ClassNode::Member::SIGNAL) {
3260+
member.signal->usages++;
3261+
}
3262+
}
3263+
}
3264+
}
3265+
}
3266+
#endif
3267+
32483268
p_call->set_datatype(call_type);
32493269
return;
32503270
} else if (GDScriptUtilityFunctions::function_exists(function_name)) {
@@ -3479,6 +3499,20 @@ void GDScriptAnalyzer::reduce_call(GDScriptParser::CallNode *p_call, bool p_is_a
34793499

34803500
parser->push_warning(p_call, GDScriptWarning::STATIC_CALLED_ON_INSTANCE, p_call->function_name, caller_type);
34813501
}
3502+
3503+
// Consider `emit_signal()`, `connect()`, and `disconnect()` as implicit uses of the signal.
3504+
if (is_self && (p_call->function_name == SNAME("emit_signal") || p_call->function_name == SNAME("connect") || p_call->function_name == SNAME("disconnect")) && !p_call->arguments.is_empty()) {
3505+
const GDScriptParser::ExpressionNode *signal_arg = p_call->arguments[0];
3506+
if (signal_arg && signal_arg->is_constant) {
3507+
const StringName &signal_name = signal_arg->reduced_value;
3508+
if (parser->current_class->has_member(signal_name)) {
3509+
const GDScriptParser::ClassNode::Member &member = parser->current_class->get_member(signal_name);
3510+
if (member.type == GDScriptParser::ClassNode::Member::SIGNAL) {
3511+
member.signal->usages++;
3512+
}
3513+
}
3514+
}
3515+
}
34823516
#endif // DEBUG_ENABLED
34833517

34843518
call_type = return_type;
Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,29 @@
1-
signal s1()
2-
signal s2()
3-
signal s3()
1+
# Doesn't produce the warning:
2+
signal used_as_first_class_signal()
3+
signal used_with_signal_constructor()
4+
signal used_with_signal_emit()
5+
signal used_with_object_emit_signal()
6+
signal used_with_object_connect()
7+
signal used_with_object_disconnect()
8+
signal used_with_self_prefix()
9+
10+
# Produce the warning:
11+
signal used_with_dynamic_name()
12+
signal just_unused()
413
@warning_ignore("unused_signal")
5-
signal s4()
14+
signal unused_but_ignored()
615

716
func no_exec():
8-
s1.emit()
9-
print(s2)
17+
print(used_as_first_class_signal)
18+
print(Signal(self, "used_with_signal_constructor"))
19+
used_with_signal_emit.emit()
20+
print(emit_signal("used_with_object_emit_signal"))
21+
print(connect("used_with_object_connect", Callable()))
22+
disconnect("used_with_object_disconnect", Callable())
23+
print(self.emit_signal("used_with_self_prefix"))
24+
25+
var dynamic_name := "used_with_dynamic_name"
26+
print(emit_signal(dynamic_name))
1027

1128
func test():
1229
pass
Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
GDTEST_OK
22
>> WARNING
3-
>> Line: 3
3+
>> Line: 11
44
>> UNUSED_SIGNAL
5-
>> The signal "s3" is declared but never explicitly used in the class.
5+
>> The signal "used_with_dynamic_name" is declared but never explicitly used in the class.
6+
>> WARNING
7+
>> Line: 12
8+
>> UNUSED_SIGNAL
9+
>> The signal "just_unused" is declared but never explicitly used in the class.

0 commit comments

Comments
 (0)