Browse Source

GDScript: Do not allow standalone lambdas

They cannot be accessed in this case, so an error is shown to avoid
misleading the uses, especially in case of named lambdas.
George Marques 3 years ago
parent
commit
0a28b4cd94

+ 4 - 0
modules/gdscript/gdscript_parser.cpp

@@ -1624,6 +1624,10 @@ GDScriptParser::Node *GDScriptParser::parse_statement() {
 					case Node::AWAIT:
 					case Node::AWAIT:
 						// Fine.
 						// Fine.
 						break;
 						break;
+					case Node::LAMBDA:
+						// Standalone lambdas can't be used, so make this an error.
+						push_error("Standalone lambdas cannot be accessed. Consider assigning it to a variable.", expression);
+						break;
 					default:
 					default:
 						push_warning(expression, GDScriptWarning::STANDALONE_EXPRESSION);
 						push_warning(expression, GDScriptWarning::STANDALONE_EXPRESSION);
 				}
 				}

+ 3 - 0
modules/gdscript/tests/scripts/parser/errors/lambda_standalone.gd

@@ -0,0 +1,3 @@
+func test():
+	func standalone():
+		print("can't be accessed")

+ 2 - 0
modules/gdscript/tests/scripts/parser/errors/lambda_standalone.out

@@ -0,0 +1,2 @@
+GDTEST_PARSER_ERROR
+Standalone lambdas cannot be accessed. Consider assigning it to a variable.