ruby-changes:52522
From: nobu <ko1@a...>
Date: Thu, 13 Sep 2018 20:10:30 +0900 (JST)
Subject: [ruby-changes:52522] nobu:r64733 (trunk): warn unused blocks with Enumerable#all? any? one? none?
nobu 2018-09-13 20:10:24 +0900 (Thu, 13 Sep 2018) New Revision: 64733 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=64733 Log: warn unused blocks with Enumerable#all? any? one? none? [Fix GH-1953] From: Koji Onishi <fursich0@g...> Modified files: trunk/array.c trunk/enum.c trunk/hash.c trunk/test/ruby/test_enum.rb Index: enum.c =================================================================== --- enum.c (revision 64732) +++ enum.c (revision 64733) @@ -1212,6 +1212,12 @@ name##_eqq(RB_BLOCK_CALL_FUNC_ARGLIST(i, https://github.com/ruby/ruby/blob/trunk/enum.c#L1212 static VALUE \ enum_##name##_func(VALUE result, struct MEMO *memo) +#define WARN_UNUSED_BLOCK(argc) do { \ + if ((argc) > 0 && rb_block_given_p()) { \ + rb_warn("given block not used"); \ + } \ +} while (0) + DEFINE_ENUMFUNCS(all) { if (!RTEST(result)) { @@ -1249,6 +1255,7 @@ static VALUE https://github.com/ruby/ruby/blob/trunk/enum.c#L1255 enum_all(int argc, VALUE *argv, VALUE obj) { struct MEMO *memo = MEMO_ENUM_NEW(Qtrue); + WARN_UNUSED_BLOCK(argc); rb_block_call(obj, id_each, 0, 0, ENUMFUNC(all), (VALUE)memo); return memo->v1; } @@ -1290,6 +1297,7 @@ static VALUE https://github.com/ruby/ruby/blob/trunk/enum.c#L1297 enum_any(int argc, VALUE *argv, VALUE obj) { struct MEMO *memo = MEMO_ENUM_NEW(Qfalse); + WARN_UNUSED_BLOCK(argc); rb_block_call(obj, id_each, 0, 0, ENUMFUNC(any), (VALUE)memo); return memo->v1; } @@ -1557,6 +1565,7 @@ enum_one(int argc, VALUE *argv, VALUE ob https://github.com/ruby/ruby/blob/trunk/enum.c#L1565 struct MEMO *memo = MEMO_ENUM_NEW(Qundef); VALUE result; + WARN_UNUSED_BLOCK(argc); rb_block_call(obj, id_each, 0, 0, ENUMFUNC(one), (VALUE)memo); result = memo->v1; if (result == Qundef) return Qfalse; @@ -1598,6 +1607,8 @@ static VALUE https://github.com/ruby/ruby/blob/trunk/enum.c#L1607 enum_none(int argc, VALUE *argv, VALUE obj) { struct MEMO *memo = MEMO_ENUM_NEW(Qtrue); + + WARN_UNUSED_BLOCK(argc); rb_block_call(obj, id_each, 0, 0, ENUMFUNC(none), (VALUE)memo); return memo->v1; } Index: test/ruby/test_enum.rb =================================================================== --- test/ruby/test_enum.rb (revision 64732) +++ test/ruby/test_enum.rb (revision 64733) @@ -314,6 +314,21 @@ class TestEnumerable < Test::Unit::TestC https://github.com/ruby/ruby/blob/trunk/test/ruby/test_enum.rb#L314 assert_equal(false, @obj.all?(1..2)) end + def test_all_with_unused_block + assert_in_out_err [], <<-EOS, [], ["-:1: warning: given block not used"] + [1, 2].all?(1) {|x| x == 3 } + EOS + assert_in_out_err [], <<-EOS, [], ["-:1: warning: given block not used"] + (1..2).all?(1) {|x| x == 3 } + EOS + assert_in_out_err [], <<-EOS, [], ["-:1: warning: given block not used"] + 3.times.all?(1) {|x| x == 3 } + EOS + assert_in_out_err [], <<-EOS, [], ["-:1: warning: given block not used"] + {a: 1, b: 2}.all?([:b, 2]) {|x| x == 4 } + EOS + end + def test_any assert_equal(true, @obj.any? {|x| x >= 3 }) assert_equal(false, @obj.any? {|x| x > 3 }) @@ -329,6 +344,21 @@ class TestEnumerable < Test::Unit::TestC https://github.com/ruby/ruby/blob/trunk/test/ruby/test_enum.rb#L344 assert_equal(true, {a: 1, b: 2}.any?(->(kv) { kv == [:b, 2] })) end + def test_any_with_unused_block + assert_in_out_err [], <<-EOS, [], ["-:1: warning: given block not used"] + [1, 23].any?(1) {|x| x == 1 } + EOS + assert_in_out_err [], <<-EOS, [], ["-:1: warning: given block not used"] + (1..2).any?(34) {|x| x == 2 } + EOS + assert_in_out_err [], <<-EOS, [], ["-:1: warning: given block not used"] + 3.times.any?(1) {|x| x == 3 } + EOS + assert_in_out_err [], <<-EOS, [], ["-:1: warning: given block not used"] + {a: 1, b: 2}.any?([:b, 2]) {|x| x == 4 } + EOS + end + def test_one assert(@obj.one? {|x| x == 3 }) assert(!(@obj.one? {|x| x == 1 })) @@ -348,6 +378,21 @@ class TestEnumerable < Test::Unit::TestC https://github.com/ruby/ruby/blob/trunk/test/ruby/test_enum.rb#L378 assert([ nil, true, 99 ].one?(Integer)) end + def test_one_with_unused_block + assert_in_out_err [], <<-EOS, [], ["-:1: warning: given block not used"] + [1, 2].one?(1) {|x| x == 3 } + EOS + assert_in_out_err [], <<-EOS, [], ["-:1: warning: given block not used"] + (1..2).one?(1) {|x| x == 3 } + EOS + assert_in_out_err [], <<-EOS, [], ["-:1: warning: given block not used"] + 3.times.one?(1) {|x| x == 3 } + EOS + assert_in_out_err [], <<-EOS, [], ["-:1: warning: given block not used"] + {a: 1, b: 2}.one?([:b, 2]) {|x| x == 4 } + EOS + end + def test_none assert(@obj.none? {|x| x == 4 }) assert(!(@obj.none? {|x| x == 1 })) @@ -365,6 +410,21 @@ class TestEnumerable < Test::Unit::TestC https://github.com/ruby/ruby/blob/trunk/test/ruby/test_enum.rb#L410 assert(@empty.none?) end + def test_none_with_unused_block + assert_in_out_err [], <<-EOS, [], ["-:1: warning: given block not used"] + [1, 2].none?(1) {|x| x == 3 } + EOS + assert_in_out_err [], <<-EOS, [], ["-:1: warning: given block not used"] + (1..2).none?(1) {|x| x == 3 } + EOS + assert_in_out_err [], <<-EOS, [], ["-:1: warning: given block not used"] + 3.times.none?(1) {|x| x == 3 } + EOS + assert_in_out_err [], <<-EOS, [], ["-:1: warning: given block not used"] + {a: 1, b: 2}.none?([:b, 2]) {|x| x == 4 } + EOS + end + def test_min assert_equal(1, @obj.min) assert_equal(3, @obj.min {|a,b| b <=> a }) Index: hash.c =================================================================== --- hash.c (revision 64732) +++ hash.c (revision 64733) @@ -3034,6 +3034,9 @@ rb_hash_any_p(int argc, VALUE *argv, VAL https://github.com/ruby/ruby/blob/trunk/hash.c#L3034 rb_check_arity(argc, 0, 1); if (RHASH_EMPTY_P(hash)) return Qfalse; if (argc) { + if (rb_block_given_p()) { + rb_warn("given block not used"); + } args[1] = argv[0]; rb_hash_foreach(hash, any_p_i_pattern, (VALUE)args); Index: array.c =================================================================== --- array.c (revision 64732) +++ array.c (revision 64733) @@ -5809,6 +5809,9 @@ rb_ary_any_p(int argc, VALUE *argv, VALU https://github.com/ruby/ruby/blob/trunk/array.c#L5809 rb_check_arity(argc, 0, 1); if (!len) return Qfalse; if (argc) { + if (rb_block_given_p()) { + rb_warn("given block not used"); + } for (i = 0; i < RARRAY_LEN(ary); ++i) { if (RTEST(rb_funcall(argv[0], idEqq, 1, RARRAY_AREF(ary, i)))) return Qtrue; } -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/