From abbdb9d9514ff1299a3a7cec044eaf735273cad3 Mon Sep 17 00:00:00 2001 From: George Marques Date: Tue, 21 Aug 2018 13:38:18 -0300 Subject: GDScript: Forbid invalid identifiers in match bindings Also forbid shadowing a variable from an upper scope. --- modules/gdscript/gdscript_parser.cpp | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) (limited to 'modules/gdscript/gdscript_parser.cpp') diff --git a/modules/gdscript/gdscript_parser.cpp b/modules/gdscript/gdscript_parser.cpp index bec314866d..841bb3e610 100644 --- a/modules/gdscript/gdscript_parser.cpp +++ b/modules/gdscript/gdscript_parser.cpp @@ -2024,12 +2024,20 @@ GDScriptParser::PatternNode *GDScriptParser::_parse_pattern(bool p_static) { // bind case GDScriptTokenizer::TK_PR_VAR: { tokenizer->advance(); + if (!tokenizer->is_token_literal()) { + _set_error("Expected identifier for binding variable name."); + return NULL; + } pattern->pt_type = GDScriptParser::PatternNode::PT_BIND; pattern->bind = tokenizer->get_token_identifier(); - // Check if binding is already used - if (current_block->variables.has(pattern->bind)) { - _set_error("Binding name of '" + pattern->bind.operator String() + "' was already used in the pattern."); - return NULL; + // Check if variable name is already used + BlockNode *bl = current_block; + while (bl) { + if (bl->variables.has(pattern->bind)) { + _set_error("Binding name of '" + pattern->bind.operator String() + "' is already declared in this scope."); + return NULL; + } + bl = bl->parent_block; } // Create local variable for proper identifier detection later LocalVarNode *lv = alloc_node(); -- cgit v1.2.3 From 94d662ad558c80b0b04c85ad3daadb24ce653cd9 Mon Sep 17 00:00:00 2001 From: George Marques Date: Tue, 21 Aug 2018 20:19:35 -0300 Subject: GDScript: Ignore unused arguments/local vars that start with _ Makes it simple to ignore particular arguments without adding special comments, especially in engine-defined functions. --- modules/gdscript/gdscript_parser.cpp | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) (limited to 'modules/gdscript/gdscript_parser.cpp') diff --git a/modules/gdscript/gdscript_parser.cpp b/modules/gdscript/gdscript_parser.cpp index 841bb3e610..2c0d541d8f 100644 --- a/modules/gdscript/gdscript_parser.cpp +++ b/modules/gdscript/gdscript_parser.cpp @@ -7397,7 +7397,7 @@ void GDScriptParser::_check_function_types(FunctionNode *p_function) { } } #ifdef DEBUG_ENABLED - if (p_function->arguments_usage[i] == 0) { + 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()); } #endif // DEBUG_ENABLED @@ -7855,10 +7855,12 @@ void GDScriptParser::_check_block_types(BlockNode *p_block) { // Warnings check for (Map::Element *E = p_block->variables.front(); E; E = E->next()) { LocalVarNode *lv = E->get(); - if (lv->usages == 0) { - _add_warning(GDScriptWarning::UNUSED_VARIABLE, lv->line, lv->name); - } else if (lv->assignments == 0) { - _add_warning(GDScriptWarning::UNASSIGNED_VARIABLE, lv->line, lv->name); + if (!lv->name.operator String().begins_with("_")) { + if (lv->usages == 0) { + _add_warning(GDScriptWarning::UNUSED_VARIABLE, lv->line, lv->name); + } else if (lv->assignments == 0) { + _add_warning(GDScriptWarning::UNASSIGNED_VARIABLE, lv->line, lv->name); + } } } #endif // DEBUG_ENABLED -- cgit v1.2.3