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

Warn also numbered parameter like parameters

[Feature #16433]
This commit is contained in:
Nobuyoshi Nakada 2019-12-20 01:01:17 +09:00
parent c6c67254fb
commit 27ddb6e998
No known key found for this signature in database
GPG key ID: 4BC7D6DF58D8DF60
3 changed files with 13 additions and 5 deletions

14
parse.y
View file

@ -11829,19 +11829,25 @@ node_newnode_with_locals(struct parser_params *p, enum node_type type, VALUE a1,
#endif #endif
static void
numparam_name(struct parser_params *p, ID id)
{
if (!NUMPARAM_ID_P(id)) return;
rb_warn1("`_%d' is used as numbered parameter",
WARN_I(NUMPARAM_ID_TO_IDX(id)));
}
static void static void
arg_var(struct parser_params *p, ID id) arg_var(struct parser_params *p, ID id)
{ {
numparam_name(p, id);
vtable_add(p->lvtbl->args, id); vtable_add(p->lvtbl->args, id);
} }
static void static void
local_var(struct parser_params *p, ID id) local_var(struct parser_params *p, ID id)
{ {
if (NUMPARAM_ID_P(id)) { numparam_name(p, id);
rb_warn1("`_%d' is used as numbered parameter",
WARN_I(NUMPARAM_ID_TO_IDX(id)));
}
vtable_add(p->lvtbl->vars, id); vtable_add(p->lvtbl->vars, id);
if (p->lvtbl->used) { if (p->lvtbl->used) {
vtable_add(p->lvtbl->used, (ID)p->ruby_sourceline); vtable_add(p->lvtbl->used, (ID)p->ruby_sourceline);

View file

@ -31,7 +31,7 @@ class TestRipper::ScannerEvents < Test::Unit::TestCase
alias compile_error on_error alias compile_error on_error
end end
end end
lexer.lex.select {|_1,type,_2| type == sym }.map {|_1,_2,tok| tok } lexer.lex.select {|_,type,_| type == sym }.map {|_,_,tok| tok }
end end
def test_tokenize def test_tokenize

View file

@ -1439,6 +1439,8 @@ eom
assert_syntax_error('proc {_1; _1 = nil}', /Can't assign to numbered parameter _1/) assert_syntax_error('proc {_1; _1 = nil}', /Can't assign to numbered parameter _1/)
assert_warn(/`_1' is used as numbered parameter/) {eval('proc {_1 = nil}')} assert_warn(/`_1' is used as numbered parameter/) {eval('proc {_1 = nil}')}
assert_warn(/`_2' is used as numbered parameter/) {eval('_2=1')} assert_warn(/`_2' is used as numbered parameter/) {eval('_2=1')}
assert_warn(/`_3' is used as numbered parameter/) {eval('proc {|_3|}')}
assert_warn(/`_4' is used as numbered parameter/) {instance_eval('def x(_4) end')}
assert_raise_with_message(NameError, /undefined local variable or method `_1'/) { assert_raise_with_message(NameError, /undefined local variable or method `_1'/) {
eval('_1') eval('_1')
} }