1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

Warn if using return at top-level with an argument

Fixes [Bug #14062]
This commit is contained in:
Jeremy Evans 2019-07-26 15:14:49 -07:00
parent c2428b8bf6
commit aa97410b0a
3 changed files with 26 additions and 6 deletions

View file

@ -6372,6 +6372,9 @@ compile_return(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const node,
switch (t) { switch (t) {
case ISEQ_TYPE_TOP: case ISEQ_TYPE_TOP:
case ISEQ_TYPE_MAIN: case ISEQ_TYPE_MAIN:
if (retval) {
rb_warn("argument of top-level return is ignored");
}
if (is == iseq) { if (is == iseq) {
/* plain top-level, leave directly */ /* plain top-level, leave directly */
type = ISEQ_TYPE_METHOD; type = ISEQ_TYPE_METHOD;

View file

@ -484,13 +484,26 @@ describe "The return keyword" do
end end
describe "return with argument" do describe "return with argument" do
# https://bugs.ruby-lang.org/issues/14062 ruby_version_is ""..."2.7" do
it "does not affect exit status" do it "does not affect exit status" do
ruby_exe(<<-END_OF_CODE).should == "" ruby_exe(<<-END_OF_CODE).should == ""
return 10 return 10
END_OF_CODE END_OF_CODE
$?.exitstatus.should == 0 $?.exitstatus.should == 0
end
end
ruby_version_is "2.7" do
it "warns but does not affect exit status" do
ruby_exe(<<-END_OF_CODE).should == "-e: warning: argument of top-level return is ignored\n"
$stderr.reopen($stdout)
system(ENV['RUBY_EXE'], '-e', 'return 10')
exit($?.exitstatus)
END_OF_CODE
$?.exitstatus.should == 0
end
end end
end end
end end

View file

@ -1195,6 +1195,10 @@ eom
end end
end end
def test_return_toplevel_with_argument
assert_warn(/argument of top-level return is ignored/) {eval("return 1")}
end
def test_syntax_error_in_rescue def test_syntax_error_in_rescue
bug12613 = '[ruby-core:76531] [Bug #12613]' bug12613 = '[ruby-core:76531] [Bug #12613]'
assert_syntax_error("#{<<-"begin;"}\n#{<<-"end;"}", /Invalid retry/, bug12613) assert_syntax_error("#{<<-"begin;"}\n#{<<-"end;"}", /Invalid retry/, bug12613)