From 384fda18b885872aacf225ac6ad7805fb101e497 Mon Sep 17 00:00:00 2001 From: nobu Date: Thu, 13 Sep 2018 11:10:24 +0000 Subject: warn unused blocks with Enumerable#all? any? one? none? [Fix GH-1953] From: Koji Onishi git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64733 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- array.c | 3 +++ enum.c | 11 +++++++++ hash.c | 3 +++ test/ruby/test_enum.rb | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 77 insertions(+) diff --git a/array.c b/array.c index 3c84a7b0b8..813605226f 100644 --- a/array.c +++ b/array.c @@ -5809,6 +5809,9 @@ rb_ary_any_p(int argc, VALUE *argv, VALUE ary) 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; } diff --git a/enum.c b/enum.c index 293ddd4b54..127b839f2d 100644 --- a/enum.c +++ b/enum.c @@ -1212,6 +1212,12 @@ name##_eqq(RB_BLOCK_CALL_FUNC_ARGLIST(i, memo)) \ 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 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 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 obj) 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 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; } diff --git a/hash.c b/hash.c index d58e67cf87..32efd02d5a 100644 --- a/hash.c +++ b/hash.c @@ -3034,6 +3034,9 @@ rb_hash_any_p(int argc, VALUE *argv, VALUE hash) 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); diff --git a/test/ruby/test_enum.rb b/test/ruby/test_enum.rb index 2167271886..5c5d359b82 100644 --- a/test/ruby/test_enum.rb +++ b/test/ruby/test_enum.rb @@ -314,6 +314,21 @@ class TestEnumerable < Test::Unit::TestCase 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::TestCase 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::TestCase 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::TestCase 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 }) -- cgit v1.2.3