Fix for not being able to ignore shadowing warnings on class scope

This commit is contained in:
jpcerrone 2023-04-03 10:57:41 -03:00
parent 91258e52be
commit 13c73500ab
7 changed files with 41 additions and 48 deletions

View File

@ -1553,7 +1553,7 @@ void GDScriptAnalyzer::resolve_function_signature(GDScriptParser::FunctionNode *
} }
parser->push_warning(p_function->parameters[i]->identifier, GDScriptWarning::UNUSED_PARAMETER, visible_name, p_function->parameters[i]->identifier->name); parser->push_warning(p_function->parameters[i]->identifier, GDScriptWarning::UNUSED_PARAMETER, visible_name, p_function->parameters[i]->identifier->name);
} }
is_shadowing(p_function->parameters[i]->identifier, "function parameter"); is_shadowing(p_function->parameters[i]->identifier, "function parameter", true);
#endif // DEBUG_ENABLED #endif // DEBUG_ENABLED
#ifdef TOOLS_ENABLED #ifdef TOOLS_ENABLED
if (p_function->parameters[i]->initializer) { if (p_function->parameters[i]->initializer) {
@ -1874,9 +1874,8 @@ void GDScriptAnalyzer::resolve_variable(GDScriptParser::VariableNode *p_variable
} else if (p_variable->assignments == 0) { } else if (p_variable->assignments == 0) {
parser->push_warning(p_variable, GDScriptWarning::UNASSIGNED_VARIABLE, p_variable->identifier->name); parser->push_warning(p_variable, GDScriptWarning::UNASSIGNED_VARIABLE, p_variable->identifier->name);
} }
is_shadowing(p_variable->identifier, kind);
} }
is_shadowing(p_variable->identifier, kind, p_is_local);
#endif #endif
} }
@ -1889,9 +1888,8 @@ void GDScriptAnalyzer::resolve_constant(GDScriptParser::ConstantNode *p_constant
if (p_constant->usages == 0) { if (p_constant->usages == 0) {
parser->push_warning(p_constant, GDScriptWarning::UNUSED_LOCAL_CONSTANT, p_constant->identifier->name); parser->push_warning(p_constant, GDScriptWarning::UNUSED_LOCAL_CONSTANT, p_constant->identifier->name);
} }
is_shadowing(p_constant->identifier, kind);
} }
is_shadowing(p_constant->identifier, kind, p_is_local);
#endif #endif
} }
@ -2052,7 +2050,7 @@ void GDScriptAnalyzer::resolve_for(GDScriptParser::ForNode *p_for) {
p_for->set_datatype(p_for->loop->get_datatype()); p_for->set_datatype(p_for->loop->get_datatype());
#ifdef DEBUG_ENABLED #ifdef DEBUG_ENABLED
if (p_for->variable) { if (p_for->variable) {
is_shadowing(p_for->variable, R"("for" iterator variable)"); is_shadowing(p_for->variable, R"("for" iterator variable)", true);
} }
#endif #endif
} }
@ -2148,7 +2146,7 @@ void GDScriptAnalyzer::resolve_match_pattern(GDScriptParser::PatternNode *p_matc
} }
p_match_pattern->bind->set_datatype(result); p_match_pattern->bind->set_datatype(result);
#ifdef DEBUG_ENABLED #ifdef DEBUG_ENABLED
is_shadowing(p_match_pattern->bind, "pattern bind"); is_shadowing(p_match_pattern->bind, "pattern bind", true);
if (p_match_pattern->bind->usages == 0 && !String(p_match_pattern->bind->name).begins_with("_")) { if (p_match_pattern->bind->usages == 0 && !String(p_match_pattern->bind->name).begins_with("_")) {
parser->push_warning(p_match_pattern->bind, GDScriptWarning::UNUSED_VARIABLE, p_match_pattern->bind->name); parser->push_warning(p_match_pattern->bind, GDScriptWarning::UNUSED_VARIABLE, p_match_pattern->bind->name);
} }
@ -4890,8 +4888,8 @@ void GDScriptAnalyzer::validate_call_arg(const List<GDScriptParser::DataType> &p
} }
#ifdef DEBUG_ENABLED #ifdef DEBUG_ENABLED
void GDScriptAnalyzer::is_shadowing(GDScriptParser::IdentifierNode *p_local, const String &p_context) { void GDScriptAnalyzer::is_shadowing(GDScriptParser::IdentifierNode *p_identifier, const String &p_context, const bool p_in_local_scope) {
const StringName &name = p_local->name; const StringName &name = p_identifier->name;
GDScriptParser::DataType base = parser->current_class->get_datatype(); GDScriptParser::DataType base = parser->current_class->get_datatype();
GDScriptParser::ClassNode *base_class = base.class_type; GDScriptParser::ClassNode *base_class = base.class_type;
@ -4901,29 +4899,30 @@ void GDScriptAnalyzer::is_shadowing(GDScriptParser::IdentifierNode *p_local, con
for (MethodInfo &info : gdscript_funcs) { for (MethodInfo &info : gdscript_funcs) {
if (info.name == name) { if (info.name == name) {
parser->push_warning(p_local, GDScriptWarning::SHADOWED_GLOBAL_IDENTIFIER, p_context, name, "built-in function"); parser->push_warning(p_identifier, GDScriptWarning::SHADOWED_GLOBAL_IDENTIFIER, p_context, name, "built-in function");
return; return;
} }
} }
if (Variant::has_utility_function(name)) { if (Variant::has_utility_function(name)) {
parser->push_warning(p_local, GDScriptWarning::SHADOWED_GLOBAL_IDENTIFIER, p_context, name, "built-in function"); parser->push_warning(p_identifier, GDScriptWarning::SHADOWED_GLOBAL_IDENTIFIER, p_context, name, "built-in function");
return; return;
} else if (ClassDB::class_exists(name)) { } else if (ClassDB::class_exists(name)) {
parser->push_warning(p_local, GDScriptWarning::SHADOWED_GLOBAL_IDENTIFIER, p_context, name, "global class"); parser->push_warning(p_identifier, GDScriptWarning::SHADOWED_GLOBAL_IDENTIFIER, p_context, name, "global class");
return; return;
} else if (GDScriptParser::get_builtin_type(name) != Variant::VARIANT_MAX) { } else if (GDScriptParser::get_builtin_type(name) != Variant::VARIANT_MAX) {
parser->push_warning(p_local, GDScriptWarning::SHADOWED_GLOBAL_IDENTIFIER, p_context, name, "built-in type"); parser->push_warning(p_identifier, GDScriptWarning::SHADOWED_GLOBAL_IDENTIFIER, p_context, name, "built-in type");
return; return;
} }
} }
while (base_class != nullptr) { if (p_in_local_scope) {
if (base_class->has_member(name)) { while (base_class != nullptr) {
parser->push_warning(p_local, GDScriptWarning::SHADOWED_VARIABLE, p_context, p_local->name, base_class->get_member(name).get_type_name(), itos(base_class->get_member(name).get_line())); if (base_class->has_member(name)) {
return; parser->push_warning(p_identifier, GDScriptWarning::SHADOWED_VARIABLE, p_context, p_identifier->name, base_class->get_member(name).get_type_name(), itos(base_class->get_member(name).get_line()));
return;
}
base_class = base_class->base_type.class_type;
} }
base_class = base_class->base_type.class_type;
} }
StringName parent = base.native_type; StringName parent = base.native_type;
@ -4931,19 +4930,19 @@ void GDScriptAnalyzer::is_shadowing(GDScriptParser::IdentifierNode *p_local, con
ERR_FAIL_COND_MSG(!class_exists(parent), "Non-existent native base class."); ERR_FAIL_COND_MSG(!class_exists(parent), "Non-existent native base class.");
if (ClassDB::has_method(parent, name, true)) { if (ClassDB::has_method(parent, name, true)) {
parser->push_warning(p_local, GDScriptWarning::SHADOWED_VARIABLE_BASE_CLASS, p_context, p_local->name, "method", parent); parser->push_warning(p_identifier, GDScriptWarning::SHADOWED_VARIABLE_BASE_CLASS, p_context, p_identifier->name, "method", parent);
return; return;
} else if (ClassDB::has_signal(parent, name, true)) { } else if (ClassDB::has_signal(parent, name, true)) {
parser->push_warning(p_local, GDScriptWarning::SHADOWED_VARIABLE_BASE_CLASS, p_context, p_local->name, "signal", parent); parser->push_warning(p_identifier, GDScriptWarning::SHADOWED_VARIABLE_BASE_CLASS, p_context, p_identifier->name, "signal", parent);
return; return;
} else if (ClassDB::has_property(parent, name, true)) { } else if (ClassDB::has_property(parent, name, true)) {
parser->push_warning(p_local, GDScriptWarning::SHADOWED_VARIABLE_BASE_CLASS, p_context, p_local->name, "property", parent); parser->push_warning(p_identifier, GDScriptWarning::SHADOWED_VARIABLE_BASE_CLASS, p_context, p_identifier->name, "property", parent);
return; return;
} else if (ClassDB::has_integer_constant(parent, name, true)) { } else if (ClassDB::has_integer_constant(parent, name, true)) {
parser->push_warning(p_local, GDScriptWarning::SHADOWED_VARIABLE_BASE_CLASS, p_context, p_local->name, "constant", parent); parser->push_warning(p_identifier, GDScriptWarning::SHADOWED_VARIABLE_BASE_CLASS, p_context, p_identifier->name, "constant", parent);
return; return;
} else if (ClassDB::has_enum(parent, name, true)) { } else if (ClassDB::has_enum(parent, name, true)) {
parser->push_warning(p_local, GDScriptWarning::SHADOWED_VARIABLE_BASE_CLASS, p_context, p_local->name, "enum", parent); parser->push_warning(p_identifier, GDScriptWarning::SHADOWED_VARIABLE_BASE_CLASS, p_context, p_identifier->name, "enum", parent);
return; return;
} }
parent = ClassDB::get_parent_class(parent); parent = ClassDB::get_parent_class(parent);

View File

@ -133,7 +133,7 @@ class GDScriptAnalyzer {
Ref<GDScriptParserRef> get_parser_for(const String &p_path); Ref<GDScriptParserRef> get_parser_for(const String &p_path);
void reduce_identifier_from_base_set_class(GDScriptParser::IdentifierNode *p_identifier, GDScriptParser::DataType p_identifier_datatype); void reduce_identifier_from_base_set_class(GDScriptParser::IdentifierNode *p_identifier, GDScriptParser::DataType p_identifier_datatype);
#ifdef DEBUG_ENABLED #ifdef DEBUG_ENABLED
void is_shadowing(GDScriptParser::IdentifierNode *p_local, const String &p_context); void is_shadowing(GDScriptParser::IdentifierNode *p_identifier, const String &p_context, const bool p_in_local_scope);
#endif #endif
public: public:

View File

@ -783,20 +783,6 @@ void GDScriptParser::parse_class_member(T *(GDScriptParser::*p_parse_function)(b
if (member->identifier != nullptr) { if (member->identifier != nullptr) {
if (!((String)member->identifier->name).is_empty()) { // Enums may be unnamed. if (!((String)member->identifier->name).is_empty()) { // Enums may be unnamed.
#ifdef DEBUG_ENABLED
List<MethodInfo> gdscript_funcs;
GDScriptLanguage::get_singleton()->get_public_functions(&gdscript_funcs);
for (MethodInfo &info : gdscript_funcs) {
if (info.name == member->identifier->name) {
push_warning(member->identifier, GDScriptWarning::SHADOWED_GLOBAL_IDENTIFIER, p_member_kind, member->identifier->name, "built-in function");
}
}
if (Variant::has_utility_function(member->identifier->name)) {
push_warning(member->identifier, GDScriptWarning::SHADOWED_GLOBAL_IDENTIFIER, p_member_kind, member->identifier->name, "built-in function");
}
#endif
if (current_class->members_indices.has(member->identifier->name)) { if (current_class->members_indices.has(member->identifier->name)) {
push_error(vformat(R"(%s "%s" has the same name as a previously declared %s.)", p_member_kind.capitalize(), member->identifier->name, current_class->get_member(member->identifier->name).get_type_name()), member->identifier); push_error(vformat(R"(%s "%s" has the same name as a previously declared %s.)", p_member_kind.capitalize(), member->identifier->name, current_class->get_member(member->identifier->name).get_type_name()), member->identifier);
} else { } else {

View File

@ -1,7 +1,7 @@
extends Node extends Node
@onready var shorthand = $Node @onready var shorthand = $Node
@onready var call = get_node(^"Node") @onready var call_no_cast = get_node(^"Node")
@onready var shorthand_with_cast = $Node as Node @onready var shorthand_with_cast = $Node as Node
@onready var call_with_cast = get_node(^"Node") as Node @onready var call_with_cast = get_node(^"Node") as Node
@ -13,6 +13,6 @@ func _init():
func test(): func test():
# Those are expected to be `null` since `_ready()` is never called on tests. # Those are expected to be `null` since `_ready()` is never called on tests.
prints("shorthand", shorthand) prints("shorthand", shorthand)
prints("call", call) prints("call_no_cast", call_no_cast)
prints("shorthand_with_cast", shorthand_with_cast) prints("shorthand_with_cast", shorthand_with_cast)
prints("call_with_cast", call_with_cast) prints("call_with_cast", call_with_cast)

View File

@ -1,5 +1,5 @@
GDTEST_OK GDTEST_OK
shorthand <null> shorthand <null>
call <null> call_no_cast <null>
shorthand_with_cast <null> shorthand_with_cast <null>
call_with_cast <null> call_with_cast <null>

View File

@ -1,5 +1,9 @@
var member: int = 0 var member: int = 0
var print_debug := 'print_debug'
@warning_ignore("shadowed_global_identifier")
var print := 'print'
@warning_ignore("unused_variable") @warning_ignore("unused_variable")
func test(): func test():
var Array := 'Array' var Array := 'Array'

View File

@ -1,26 +1,30 @@
GDTEST_OK GDTEST_OK
>> WARNING >> WARNING
>> Line: 5 >> Line: 3
>> SHADOWED_GLOBAL_IDENTIFIER
>> The variable "print_debug" has the same name as a built-in function.
>> WARNING
>> Line: 9
>> SHADOWED_GLOBAL_IDENTIFIER >> SHADOWED_GLOBAL_IDENTIFIER
>> The variable "Array" has the same name as a built-in type. >> The variable "Array" has the same name as a built-in type.
>> WARNING >> WARNING
>> Line: 6 >> Line: 10
>> SHADOWED_GLOBAL_IDENTIFIER >> SHADOWED_GLOBAL_IDENTIFIER
>> The variable "Node" has the same name as a global class. >> The variable "Node" has the same name as a global class.
>> WARNING >> WARNING
>> Line: 7 >> Line: 11
>> SHADOWED_GLOBAL_IDENTIFIER >> SHADOWED_GLOBAL_IDENTIFIER
>> The variable "is_same" has the same name as a built-in function. >> The variable "is_same" has the same name as a built-in function.
>> WARNING >> WARNING
>> Line: 8 >> Line: 12
>> SHADOWED_GLOBAL_IDENTIFIER >> SHADOWED_GLOBAL_IDENTIFIER
>> The variable "sqrt" has the same name as a built-in function. >> The variable "sqrt" has the same name as a built-in function.
>> WARNING >> WARNING
>> Line: 9 >> Line: 13
>> SHADOWED_VARIABLE >> SHADOWED_VARIABLE
>> The local variable "member" is shadowing an already-declared variable at line 1. >> The local variable "member" is shadowing an already-declared variable at line 1.
>> WARNING >> WARNING
>> Line: 10 >> Line: 14
>> SHADOWED_VARIABLE_BASE_CLASS >> SHADOWED_VARIABLE_BASE_CLASS
>> The local variable "reference" is shadowing an already-declared method at the base class "RefCounted". >> The local variable "reference" is shadowing an already-declared method at the base class "RefCounted".
warn warn