From 48a2fd7a3d18a05c29ba8ba3b5b8afbc968499cb Mon Sep 17 00:00:00 2001 From: Tan Wang Leng Date: Tue, 4 Feb 2020 15:58:27 +0800 Subject: [PATCH] gdscript_parser: Fix "unreachable code" false positive for loops Depending on the conditional statements of the 'for' and 'while' loops, their body may not even execute once. For example: func a(): var arr = [] for i in arr: return i # can be reached, but analysis says cannot return -1 func b(): var should_loop = false while should_loop: return 1 # can be reached, but analysis says cannot return 0 The parser will complain that the statements after the comment cannot be reached, but it is clearly possible for our scenario. This is because the parser falsely assumes that the loop body will always execute at least once. Fix the code to remove this assumption for both of those loops. (cherry picked from commit 7b1423a61ea6a5249d29ddbb3866ce815f16497c) --- modules/gdscript/gdscript_parser.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/modules/gdscript/gdscript_parser.cpp b/modules/gdscript/gdscript_parser.cpp index 3efb8fe9aa3..d57da2c4197 100644 --- a/modules/gdscript/gdscript_parser.cpp +++ b/modules/gdscript/gdscript_parser.cpp @@ -3107,7 +3107,6 @@ void GDScriptParser::_parse_block(BlockNode *p_block, bool p_static) { current_block = p_block; if (error_set) return; - p_block->has_return = cf_while->body->has_return; p_block->statements.push_back(cf_while); } break; case GDScriptTokenizer::TK_CF_FOR: { @@ -3240,7 +3239,6 @@ void GDScriptParser::_parse_block(BlockNode *p_block, bool p_static) { if (error_set) return; - p_block->has_return = cf_for->body->has_return; p_block->statements.push_back(cf_for); } break; case GDScriptTokenizer::TK_CF_CONTINUE: {