GDScript: add variable shadowing warning

(cherry picked from commit 14078fbb82)
This commit is contained in:
lupoDharkael 2019-04-15 15:51:52 +02:00 committed by Rémi Verschelde
parent 66c1b8adc4
commit ad1e8069d3
3 changed files with 22 additions and 0 deletions

View File

@ -1945,6 +1945,10 @@ String GDScriptWarning::get_message() const {
CHECK_SYMBOLS(1); CHECK_SYMBOLS(1);
return "The local variable '" + symbols[0] + "' is declared but never used in the block."; return "The local variable '" + symbols[0] + "' is declared but never used in the block.";
} break; } break;
case SHADOWED_VARIABLE: {
CHECK_SYMBOLS(2);
return "The local variable '" + symbols[0] + "' is shadowing an already defined variable at line " + symbols[1] + ".";
} break;
case UNUSED_CLASS_VARIABLE: { case UNUSED_CLASS_VARIABLE: {
CHECK_SYMBOLS(1); CHECK_SYMBOLS(1);
return "The class variable '" + symbols[0] + "' is declared but never used in the script."; return "The class variable '" + symbols[0] + "' is declared but never used in the script.";
@ -2048,6 +2052,7 @@ String GDScriptWarning::get_name_from_code(Code p_code) {
"UNASSIGNED_VARIABLE", "UNASSIGNED_VARIABLE",
"UNASSIGNED_VARIABLE_OP_ASSIGN", "UNASSIGNED_VARIABLE_OP_ASSIGN",
"UNUSED_VARIABLE", "UNUSED_VARIABLE",
"SHADOWED_VARIABLE",
"UNUSED_CLASS_VARIABLE", "UNUSED_CLASS_VARIABLE",
"UNUSED_ARGUMENT", "UNUSED_ARGUMENT",
"UNREACHABLE_CODE", "UNREACHABLE_CODE",

View File

@ -273,6 +273,7 @@ struct GDScriptWarning {
UNASSIGNED_VARIABLE, // Variable used but never assigned UNASSIGNED_VARIABLE, // Variable used but never assigned
UNASSIGNED_VARIABLE_OP_ASSIGN, // Variable never assigned but used in an assignment operation (+=, *=, etc) UNASSIGNED_VARIABLE_OP_ASSIGN, // Variable never assigned but used in an assignment operation (+=, *=, etc)
UNUSED_VARIABLE, // Local variable is declared but never used UNUSED_VARIABLE, // Local variable is declared but never used
SHADOWED_VARIABLE, // Variable name shadowed by other variable
UNUSED_CLASS_VARIABLE, // Class variable is declared but never used in the file UNUSED_CLASS_VARIABLE, // Class variable is declared but never used in the file
UNUSED_ARGUMENT, // Function argument is never used UNUSED_ARGUMENT, // Function argument is never used
UNREACHABLE_CODE, // Code after a return statement UNREACHABLE_CODE, // Code after a return statement

View File

@ -7661,6 +7661,11 @@ void GDScriptParser::_check_function_types(FunctionNode *p_function) {
if (p_function->arguments_usage[i] == 0 && !p_function->arguments[i].operator String().begins_with("_")) { if (p_function->arguments_usage[i] == 0 && !p_function->arguments[i].operator String().begins_with("_")) {
_add_warning(GDScriptWarning::UNUSED_ARGUMENT, p_function->line, p_function->name, p_function->arguments[i].operator String()); _add_warning(GDScriptWarning::UNUSED_ARGUMENT, p_function->line, p_function->name, p_function->arguments[i].operator String());
} }
for (int j = 0; j < current_class->variables.size(); j++) {
if (current_class->variables[j].identifier == p_function->arguments[i]) {
_add_warning(GDScriptWarning::SHADOWED_VARIABLE, p_function->line, p_function->arguments[i], itos(current_class->variables[j].line));
}
}
#endif // DEBUG_ENABLED #endif // DEBUG_ENABLED
} }
@ -7734,6 +7739,17 @@ void GDScriptParser::_check_function_types(FunctionNode *p_function) {
p_function->return_type.has_type = false; p_function->return_type.has_type = false;
p_function->return_type.may_yield = true; p_function->return_type.may_yield = true;
} }
#ifdef DEBUG_ENABLED
for (Map<StringName, LocalVarNode *>::Element *E = p_function->body->variables.front(); E; E = E->next()) {
LocalVarNode *lv = E->get();
for (int i = 0; i < current_class->variables.size(); i++) {
if (current_class->variables[i].identifier == lv->name) {
_add_warning(GDScriptWarning::SHADOWED_VARIABLE, lv->line, lv->name, itos(current_class->variables[i].line));
}
}
}
#endif // DEBUG_ENABLED
} }
void GDScriptParser::_check_class_blocks_types(ClassNode *p_class) { void GDScriptParser::_check_class_blocks_types(ClassNode *p_class) {