summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNobuyoshi Nakada <nobu@ruby-lang.org>2022-05-24 16:51:15 +0900
committerNobuyoshi Nakada <nobu@ruby-lang.org>2022-06-20 19:35:12 +0900
commitab2a43265cfdda288d1baaa29936fd408c2a42bc (patch)
treef70d3b5b1c9ddb1c8a259642f9ede21a25afc173
parenta58611dfb1bfc74fb1a51a9cd0ca8ac690c2f1f4 (diff)
downloadruby-ab2a43265cfdda288d1baaa29936fd408c2a42bc.tar.gz
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.
-rw-r--r--NEWS.md5
-rw-r--r--common.mk3
-rw-r--r--re.c4
-rw-r--r--test/ruby/test_regexp.rb6
4 files changed, 17 insertions, 1 deletions
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.
* 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.
[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
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 @@
#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)
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
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")