1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00
* parse.y (shadowing_lvar_gen, warn_unused_var): no warnings for
  variables starting with _.  [ruby-core:46160][Feature #6693]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@36337 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2012-07-07 22:36:25 +00:00
parent cf06ed4f52
commit fbacb71b38
3 changed files with 22 additions and 7 deletions

View file

@ -1,3 +1,8 @@
Sun Jul 8 07:36:19 2012 Nobuyoshi Nakada <nobu@ruby-lang.org>
* parse.y (shadowing_lvar_gen, warn_unused_var): no warnings for
variables starting with _. [ruby-core:46160][Feature #6693]
Sat Jul 7 23:07:30 2012 CHIKANAGA Tomoyuki <nagachika@ruby-lang.org>
* test/csv/test_features.rb: add require for Tempfile.

15
parse.y
View file

@ -8595,12 +8595,23 @@ assignable_gen(struct parser_params *parser, ID id, NODE *val)
#undef parser_yyerror
}
static int
is_private_local_id(ID name)
{
VALUE s;
if (name == idUScore) return 1;
if (!is_local_id(name)) return 0;
s = rb_id2str(name);
if (!s) return 0;
return RSTRING_PTR(s)[0] == '_';
}
#define LVAR_USED ((int)1 << (sizeof(int) * CHAR_BIT - 1))
static ID
shadowing_lvar_gen(struct parser_params *parser, ID name)
{
if (idUScore == name) return name;
if (is_private_local_id(name)) return name;
if (dyna_in_block()) {
if (dvar_curr(name)) {
yyerror("duplicated argument name");
@ -9324,7 +9335,7 @@ warn_unused_var(struct parser_params *parser, struct local_vars *local)
}
for (i = 0; i < cnt; ++i) {
if (!v[i] || (u[i] & LVAR_USED)) continue;
if (idUScore == v[i]) continue;
if (is_private_local_id(v[i])) continue;
rb_compile_warn(ruby_sourcefile, (int)u[i], "assigned but unused variable - %s", rb_id2name(v[i]));
}
}

View file

@ -497,7 +497,8 @@ class TestRubyOptions < Test::Unit::TestCase
assert_in_out_err(["-we", "1.times do\n a=1\nend"], "", [], [], feature3446)
assert_in_out_err(["-we", "def foo\n 1.times do\n a=1\n end\nend"], "", [], ["-e:3: warning: assigned but unused variable - a"], feature3446)
assert_in_out_err(["-we", "def foo\n"" 1.times do |a| end\n""end"], "", [], [])
assert_in_out_err(["-we", "def foo\n _a=1\nend"], "", [], ["-e:2: warning: assigned but unused variable - _a"], feature3446)
feature6693 = '[ruby-core:46160]'
assert_in_out_err(["-we", "def foo\n _a=1\nend"], "", [], [], feature6693)
end
def test_shadowing_variable
@ -509,11 +510,9 @@ class TestRubyOptions < Test::Unit::TestCase
["-e:3: warning: shadowing outer local variable - a",
"-e:2: warning: assigned but unused variable - a",
], bug4130)
feature6693 = '[ruby-core:46160]'
assert_in_out_err(["-we", "def foo\n"" _a=1\n"" 1.times do |_a| end\n""end"],
"", [],
["-e:3: warning: shadowing outer local variable - _a",
"-e:2: warning: assigned but unused variable - _a",
], bug4130)
"", [], [], feature6693)
end
def test_script_from_stdin