ruby-changes:72255
From: Nobuyoshi <ko1@a...>
Date: Mon, 20 Jun 2022 19:35:31 +0900 (JST)
Subject: [ruby-changes:72255] ab2a43265c (master): Warn suspicious flag to `Regexp.new`
https://git.ruby-lang.org/ruby.git/commit/?id=ab2a43265c From ab2a43265cfdda288d1baaa29936fd408c2a42bc Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada <nobu@r...> Date: Tue, 24 May 2022 16:51:15 +0900 Subject: Warn suspicious flag to `Regexp.new` Now second argument should be `true`, `false`, `nil` or Integer. This flag is confused with third argument some times. --- NEWS.md | 5 +++++ common.mk | 3 +++ re.c | 4 +++- test/ruby/test_regexp.rb | 6 ++++++ 4 files changed, 17 insertions(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index 91f329aaff..02163ff9c0 100644 --- a/NEWS.md +++ b/NEWS.md @@ -118,6 +118,10 @@ Note: We're only listing outstanding class updates. https://github.com/ruby/ruby/blob/trunk/NEWS.md#L118 * Proc#dup returns an instance of subclass. [[Bug #17545]] * Proc#parameters now accepts lambda keyword. [[Feature #15357]] +* Regexp + * Regexp.new now warns second argument, other than `true`, `false`, + `nil` or Integer. [[Feature #18788]] + * Refinement * Refinement#refined_class has been added. [[Feature #12737]] @@ -263,3 +267,4 @@ The following deprecated APIs are removed. https://github.com/ruby/ruby/blob/trunk/NEWS.md#L267 [Bug #18625]: https://bugs.ruby-lang.org/issues/18625 [Bug #18633]: https://bugs.ruby-lang.org/issues/18633 [Bug #18782]: https://bugs.ruby-lang.org/issues/18782 +[Feature #18788]: https://bugs.ruby-lang.org/issues/18788 diff --git a/common.mk b/common.mk index cbe03365d1..f401785cfb 100644 --- a/common.mk +++ b/common.mk @@ -12057,12 +12057,15 @@ re.$(OBJEXT): $(hdrdir)/ruby.h https://github.com/ruby/ruby/blob/trunk/common.mk#L12057 re.$(OBJEXT): $(hdrdir)/ruby/ruby.h re.$(OBJEXT): $(top_srcdir)/internal/array.h re.$(OBJEXT): $(top_srcdir)/internal/bits.h +re.$(OBJEXT): $(top_srcdir)/internal/class.h re.$(OBJEXT): $(top_srcdir)/internal/compilers.h re.$(OBJEXT): $(top_srcdir)/internal/gc.h re.$(OBJEXT): $(top_srcdir)/internal/hash.h re.$(OBJEXT): $(top_srcdir)/internal/imemo.h +re.$(OBJEXT): $(top_srcdir)/internal/object.h re.$(OBJEXT): $(top_srcdir)/internal/ractor.h re.$(OBJEXT): $(top_srcdir)/internal/re.h +re.$(OBJEXT): $(top_srcdir)/internal/serial.h re.$(OBJEXT): $(top_srcdir)/internal/static_assert.h re.$(OBJEXT): $(top_srcdir)/internal/string.h re.$(OBJEXT): $(top_srcdir)/internal/time.h diff --git a/re.c b/re.c index e9bcf11b31..f225a413d5 100644 --- a/re.c +++ b/re.c @@ -20,6 +20,7 @@ https://github.com/ruby/ruby/blob/trunk/re.c#L20 #include "internal/imemo.h" #include "internal/re.h" #include "internal/string.h" +#include "internal/object.h" #include "internal/ractor.h" #include "internal/variable.h" #include "regint.h" @@ -3716,7 +3717,8 @@ rb_reg_initialize_m(int argc, VALUE *argv, VALUE self) https://github.com/ruby/ruby/blob/trunk/re.c#L3717 else { if (opts != Qundef) { if (FIXNUM_P(opts)) flags = FIX2INT(opts); - else if (RTEST(opts)) flags = ONIG_OPTION_IGNORECASE; + else if (!NIL_P(opts) && rb_bool_expected(opts, "ignorecase", FALSE)) + flags = ONIG_OPTION_IGNORECASE; } if (n_flag != Qundef && !NIL_P(n_flag)) { char *kcode = StringValuePtr(n_flag); diff --git a/test/ruby/test_regexp.rb b/test/ruby/test_regexp.rb index 71d56ad027..5ee6b1b03c 100644 --- a/test/ruby/test_regexp.rb +++ b/test/ruby/test_regexp.rb @@ -628,6 +628,12 @@ class TestRegexp < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_regexp.rb#L628 assert_raise(RegexpError) { Regexp.new("((?<v>))\\g<0>") } end + def test_initialize_bool_warning + assert_warning(/expected true or false as ignorecase/) do + Regexp.new("foo", :i) + end + end + def test_match_control_meta_escape assert_equal(0, /\c\xFF/ =~ "\c\xFF") assert_equal(0, /\c\M-\xFF/ =~ "\c\M-\xFF") -- cgit v1.2.1 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/