From 763577ee1219ef0986da881529a69110cf31fe7f Mon Sep 17 00:00:00 2001 From: Jeremy Evans Date: Fri, 26 Jul 2019 15:14:49 -0700 Subject: [PATCH] Warn if using return at top-level with an argument Fixes [Bug #14062] --- compile.c | 3 +++ test/ruby/test_syntax.rb | 4 ++++ 2 files changed, 7 insertions(+) diff --git a/compile.c b/compile.c index 7d71f26208..c47d3652e7 100644 --- a/compile.c +++ b/compile.c @@ -6372,6 +6372,9 @@ compile_return(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const node, switch (t) { case ISEQ_TYPE_TOP: case ISEQ_TYPE_MAIN: + if (retval) { + rb_warn("return at top-level with argument"); + } if (is == iseq) { /* plain top-level, leave directly */ type = ISEQ_TYPE_METHOD; diff --git a/test/ruby/test_syntax.rb b/test/ruby/test_syntax.rb index c5c3737b30..c829c4d065 100644 --- a/test/ruby/test_syntax.rb +++ b/test/ruby/test_syntax.rb @@ -1195,6 +1195,10 @@ def test_return_toplevel end end + def test_return_toplevel_with_argument + assert_warn(/return at top-level with argument/) {eval("return 1")} + end + def test_syntax_error_in_rescue bug12613 = '[ruby-core:76531] [Bug #12613]' assert_syntax_error("#{<<-"begin;"}\n#{<<-"end;"}", /Invalid retry/, bug12613) -- 2.21.0