Skip to content

Commit d1e2afa

Browse files
committed
GDScript: Do not produce UNUSED_SIGNAL warning for common implicit uses
1 parent fe01776 commit d1e2afa

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
@@ -3113,6 +3113,26 @@ void GDScriptAnalyzer::reduce_call(GDScriptParser::CallNode *p_call, bool p_is_a
31133113
push_error(vformat(R"(No constructor of "%s" matches the signature "%s".)", Variant::get_type_name(builtin_type), signature), p_call);
31143114
}
31153115
}
3116+
3117+
#ifdef DEBUG_ENABLED
3118+
// Consider `Signal(self, "my_signal")` as an implicit use of the signal.
3119+
if (builtin_type == Variant::SIGNAL && p_call->arguments.size() >= 2) {
3120+
const GDScriptParser::ExpressionNode *object_arg = p_call->arguments[0];
3121+
if (object_arg && object_arg->type == GDScriptParser::Node::SELF) {
3122+
const GDScriptParser::ExpressionNode *signal_arg = p_call->arguments[1];
3123+
if (signal_arg && signal_arg->is_constant) {
3124+
const StringName &signal_name = signal_arg->reduced_value;
3125+
if (parser->current_class->has_member(signal_name)) {
3126+
const GDScriptParser::ClassNode::Member &member = parser->current_class->get_member(signal_name);
3127+
if (member.type == GDScriptParser::ClassNode::Member::SIGNAL) {
3128+
member.signal->usages++;
3129+
}
3130+
}
3131+
}
3132+
}
3133+
}
3134+
#endif
3135+
31163136
p_call->set_datatype(call_type);
31173137
return;
31183138
} else if (GDScriptUtilityFunctions::function_exists(function_name)) {
@@ -3345,6 +3365,20 @@ void GDScriptAnalyzer::reduce_call(GDScriptParser::CallNode *p_call, bool p_is_a
33453365

33463366
parser->push_warning(p_call, GDScriptWarning::STATIC_CALLED_ON_INSTANCE, p_call->function_name, caller_type);
33473367
}
3368+
3369+
// Consider `emit_signal()`, `connect()`, and `disconnect()` as implicit uses of the signal.
3370+
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()) {
3371+
const GDScriptParser::ExpressionNode *signal_arg = p_call->arguments[0];
3372+
if (signal_arg && signal_arg->is_constant) {
3373+
const StringName &signal_name = signal_arg->reduced_value;
3374+
if (parser->current_class->has_member(signal_name)) {
3375+
const GDScriptParser::ClassNode::Member &member = parser->current_class->get_member(signal_name);
3376+
if (member.type == GDScriptParser::ClassNode::Member::SIGNAL) {
3377+
member.signal->usages++;
3378+
}
3379+
}
3380+
}
3381+
}
33483382
#endif // DEBUG_ENABLED
33493383

33503384
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)