1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

Prefer rb_check_arity when 0 or 1 arguments

Especially over checking argc then calling rb_scan_args just to
raise an ArgumentError.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66238 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2018-12-06 07:49:24 +00:00
parent 0cd28199e5
commit 98e65d9d92
18 changed files with 72 additions and 139 deletions

17
enum.c
View file

@ -2009,11 +2009,11 @@ enum_min_by(int argc, VALUE *argv, VALUE obj)
struct MEMO *memo;
VALUE num;
rb_scan_args(argc, argv, "01", &num);
rb_check_arity(argc, 0, 1);
RETURN_SIZED_ENUMERATOR(obj, argc, argv, enum_size);
if (!NIL_P(num))
if (argc && !NIL_P(num = argv[0]))
return rb_nmin_run(obj, num, 1, 0, 0);
memo = MEMO_NEW(Qundef, Qnil, 0);
@ -2116,11 +2116,11 @@ enum_max_by(int argc, VALUE *argv, VALUE obj)
struct MEMO *memo;
VALUE num;
rb_scan_args(argc, argv, "01", &num);
rb_check_arity(argc, 0, 1);
RETURN_SIZED_ENUMERATOR(obj, argc, argv, enum_size);
if (!NIL_P(num))
if (argc && !NIL_P(num = argv[0]))
return rb_nmin_run(obj, num, 1, 1, 0);
memo = MEMO_NEW(Qundef, Qnil, 0);
@ -2971,10 +2971,10 @@ enum_cycle(int argc, VALUE *argv, VALUE obj)
VALUE nv = Qnil;
long n, i, len;
rb_scan_args(argc, argv, "01", &nv);
rb_check_arity(argc, 0, 1);
RETURN_SIZED_ENUMERATOR(obj, argc, argv, enum_cycle_size);
if (NIL_P(nv)) {
if (!argc || NIL_P(nv = argv[0])) {
n = -1;
}
else {
@ -3944,11 +3944,8 @@ enum_sum(int argc, VALUE* argv, VALUE obj)
VALUE beg, end;
int excl;
if (rb_scan_args(argc, argv, "01", &memo.v) == 0)
memo.v = LONG2FIX(0);
memo.v = (rb_check_arity(argc, 0, 1) == 0) ? LONG2FIX(0) : argv[0];
memo.block_given = rb_block_given_p();
memo.n = 0;
memo.r = Qundef;

View file

@ -604,12 +604,9 @@ enumerator_with_index(int argc, VALUE *argv, VALUE obj)
{
VALUE memo;
rb_scan_args(argc, argv, "01", &memo);
rb_check_arity(argc, 0, 1);
RETURN_SIZED_ENUMERATOR(obj, argc, argv, enumerator_enum_size);
if (NIL_P(memo))
memo = INT2FIX(0);
else
memo = rb_to_int(memo);
memo = (!argc || NIL_P(memo = argv[0])) ? INT2FIX(0) : rb_to_int(memo);
return enumerator_block_call(obj, enumerator_with_index_i, (VALUE)MEMO_NEW(memo, 0, 0));
}

View file

@ -946,7 +946,7 @@ exc_initialize(int argc, VALUE *argv, VALUE exc)
{
VALUE arg;
rb_scan_args(argc, argv, "01", &arg);
arg = (!rb_check_arity(argc, 0, 1) ? Qnil : argv[0]);
return exc_init(exc, arg);
}

31
gc.c
View file

@ -2722,12 +2722,7 @@ os_each_obj(int argc, VALUE *argv, VALUE os)
{
VALUE of;
if (argc == 0) {
of = 0;
}
else {
rb_scan_args(argc, argv, "01", &of);
}
of = (!rb_check_arity(argc, 0, 1) ? 0 : argv[0]);
RETURN_ENUMERATOR(os, 1, &of);
return os_obj_of(of);
}
@ -3487,9 +3482,10 @@ count_objects(int argc, VALUE *argv, VALUE os)
size_t freed = 0;
size_t total = 0;
size_t i;
VALUE hash;
VALUE hash = Qnil;
if (rb_scan_args(argc, argv, "01", &hash) == 1) {
if (rb_check_arity(argc, 0, 1) == 1) {
hash = argv[0];
if (!RB_TYPE_P(hash, T_HASH))
rb_raise(rb_eTypeError, "non-hash given");
}
@ -7077,13 +7073,13 @@ gc_latest_gc_info(int argc, VALUE *argv, VALUE self)
rb_objspace_t *objspace = &rb_objspace;
VALUE arg = Qnil;
if (rb_scan_args(argc, argv, "01", &arg) == 1) {
if (rb_check_arity(argc, 0, 1) == 1) {
arg = argv[0];
if (!SYMBOL_P(arg) && !RB_TYPE_P(arg, T_HASH)) {
rb_raise(rb_eTypeError, "non-hash or symbol given");
}
}
if (arg == Qnil) {
else {
arg = rb_hash_new();
}
@ -7452,7 +7448,8 @@ gc_stat(int argc, VALUE *argv, VALUE self)
{
VALUE arg = Qnil;
if (rb_scan_args(argc, argv, "01", &arg) == 1) {
if (rb_check_arity(argc, 0, 1) == 1) {
arg = argv[0];
if (SYMBOL_P(arg)) {
size_t value = gc_stat_internal(arg);
return SIZET2NUM(value);
@ -7461,8 +7458,7 @@ gc_stat(int argc, VALUE *argv, VALUE self)
rb_raise(rb_eTypeError, "non-hash or symbol given");
}
}
if (arg == Qnil) {
else {
arg = rb_hash_new();
}
gc_stat_internal(arg);
@ -9455,12 +9451,7 @@ gc_profile_report(int argc, VALUE *argv, VALUE self)
{
VALUE out;
if (argc == 0) {
out = rb_stdout;
}
else {
rb_scan_args(argc, argv, "01", &out);
}
out = (!rb_check_arity(argc, 0, 1) ? rb_stdout : argv[0]);
gc_profile_dump_on(out, rb_io_write);
return Qnil;

7
io.c
View file

@ -7878,12 +7878,7 @@ rb_obj_display(int argc, VALUE *argv, VALUE self)
{
VALUE out;
if (argc == 0) {
out = rb_stdout;
}
else {
rb_scan_args(argc, argv, "01", &out);
}
out = (!rb_check_arity(argc, 0, 1) ? rb_stdout : argv[0]);
rb_io_write(out, self);
return Qnil;

3
iseq.c
View file

@ -3183,8 +3183,7 @@ rb_iseqw_local_variables(VALUE iseqval)
static VALUE
iseqw_to_binary(int argc, VALUE *argv, VALUE self)
{
VALUE opt;
rb_scan_args(argc, argv, "01", &opt);
VALUE opt = !rb_check_arity(argc, 0, 1) ? Qnil : argv[0];
return rb_iseq_ibf_dump(iseqw_check(self), opt);
}

3
proc.c
View file

@ -2993,8 +2993,7 @@ proc_curry(int argc, const VALUE *argv, VALUE self)
int sarity, max_arity, min_arity = rb_proc_min_max_arity(self, &max_arity);
VALUE arity;
rb_scan_args(argc, argv, "01", &arity);
if (NIL_P(arity)) {
if (rb_check_arity(argc, 0, 1) == 0 || NIL_P(arity = argv[0])) {
arity = INT2FIX(min_arity);
}
else {

View file

@ -397,12 +397,7 @@ range_step(int argc, VALUE *argv, VALUE range)
b = RANGE_BEG(range);
e = RANGE_END(range);
if (argc == 0) {
step = INT2FIX(1);
}
else {
rb_scan_args(argc, argv, "01", &step);
}
step = (!rb_check_arity(argc, 0, 1) ? INT2FIX(1) : argv[0]);
if (!rb_block_given_p()) {
if (rb_obj_is_kind_of(b, rb_cNumeric) && (NIL_P(e) || rb_obj_is_kind_of(e, rb_cNumeric))) {

View file

@ -1385,10 +1385,10 @@ f_round_common(int argc, VALUE *argv, VALUE self, VALUE (*func)(VALUE))
{
VALUE n, b, s;
if (argc == 0)
if (rb_check_arity(argc, 0, 1) == 0)
return (*func)(self);
rb_scan_args(argc, argv, "01", &n);
n = argv[0];
if (!k_integer_p(n))
rb_raise(rb_eTypeError, "not an integer");
@ -1711,14 +1711,13 @@ nurat_rationalize(int argc, VALUE *argv, VALUE self)
{
VALUE e, a, b, p, q;
if (argc == 0)
if (rb_check_arity(argc, 0, 1) == 0)
return self;
if (nurat_negative_p(self))
return rb_rational_uminus(nurat_rationalize(argc, argv, rb_rational_uminus(self)));
rb_scan_args(argc, argv, "01", &e);
e = f_abs(e);
e = f_abs(argv[0]);
a = f_sub(self, e);
b = f_add(self, e);
@ -2283,16 +2282,13 @@ rb_flt_rationalize(VALUE flt)
static VALUE
float_rationalize(int argc, VALUE *argv, VALUE self)
{
VALUE e;
double d = RFLOAT_VALUE(self);
if (d < 0.0)
return rb_rational_uminus(float_rationalize(argc, argv, DBL2NUM(-d)));
rb_scan_args(argc, argv, "01", &e);
if (argc != 0) {
return rb_flt_rationalize_with_prec(self, e);
if (rb_check_arity(argc, 0, 1)) {
return rb_flt_rationalize_with_prec(self, argv[0]);
}
else {
return rb_flt_rationalize(self);

View file

@ -391,7 +391,7 @@ interrupt_init(int argc, VALUE *argv, VALUE self)
VALUE args[2];
args[0] = INT2FIX(SIGINT);
rb_scan_args(argc, argv, "01", &args[1]);
args[1] = rb_check_arity(argc, 0, 1) ? argv[0] : Qnil;
return rb_call_super(2, args);
}

View file

@ -5724,16 +5724,9 @@ rb_str_include(VALUE str, VALUE arg)
static VALUE
rb_str_to_i(int argc, VALUE *argv, VALUE str)
{
int base;
int base = 10;
if (argc == 0) base = 10;
else {
VALUE b;
rb_scan_args(argc, argv, "01", &b);
base = NUM2INT(b);
}
if (base < 0) {
if (rb_check_arity(argc, 0, 1) && (base = NUM2INT(argv[0])) < 0) {
rb_raise(rb_eArgError, "invalid radix %d", base);
}
return rb_str_to_inum(str, base, FALSE);
@ -9369,20 +9362,14 @@ static VALUE
rb_str_sum(int argc, VALUE *argv, VALUE str)
{
VALUE vbits;
int bits;
int bits = 16;
char *ptr, *p, *pend;
long len;
VALUE sum = INT2FIX(0);
unsigned long sum0 = 0;
if (argc == 0) {
bits = 16;
}
else {
rb_scan_args(argc, argv, "01", &vbits);
bits = NUM2INT(vbits);
if (bits < 0)
bits = 0;
if (rb_check_arity(argc, 0, 1) && (bits = NUM2INT(argv[0])) < 0) {
bits = 0;
}
ptr = p = RSTRING_PTR(str);
len = RSTRING_LEN(str);
@ -10397,7 +10384,7 @@ unicode_normalize_common(int argc, VALUE *argv, VALUE str, ID id)
UnicodeNormalizeRequired = 1;
}
argv2[0] = str;
rb_scan_args(argc, argv, "01", &argv2[1]);
if (rb_check_arity(argc, 0, 1)) argv2[1] = argv[0];
return rb_funcallv(mUnicodeNormalize, id, argc+1, argv2);
}

View file

@ -1111,19 +1111,18 @@ thread_join_m(int argc, VALUE *argv, VALUE self)
VALUE limit;
rb_hrtime_t rel, *to = 0;
rb_scan_args(argc, argv, "01", &limit);
/*
* This supports INFINITY and negative values, so we can't use
* rb_time_interval right now...
*/
switch (TYPE(limit)) {
case T_NIL: break;
case T_FIXNUM:
if (!rb_check_arity(argc, 0, 1) || NIL_P(argv[0])) {
/* unlimited */
}
else if (FIXNUM_P(limit = argv[0])) {
rel = rb_sec2hrtime(NUM2TIMET(limit));
to = &rel;
break;
default:
}
else {
to = double2hrtime(&rel, rb_num2dbl(limit));
}
@ -2041,20 +2040,19 @@ rb_thread_pending_interrupt_p(int argc, VALUE *argv, VALUE target_thread)
if (rb_threadptr_pending_interrupt_empty_p(target_th)) {
return Qfalse;
}
if (rb_check_arity(argc, 0, 1)) {
VALUE err = argv[0];
if (!rb_obj_is_kind_of(err, rb_cModule)) {
rb_raise(rb_eTypeError, "class or module required for rescue clause");
}
if (rb_threadptr_pending_interrupt_include_p(target_th, err)) {
return Qtrue;
}
else {
return Qfalse;
}
}
else {
if (argc == 1) {
VALUE err;
rb_scan_args(argc, argv, "01", &err);
if (!rb_obj_is_kind_of(err, rb_cModule)) {
rb_raise(rb_eTypeError, "class or module required for rescue clause");
}
if (rb_threadptr_pending_interrupt_include_p(target_th, err)) {
return Qtrue;
}
else {
return Qfalse;
}
}
return Qtrue;
}
}

View file

@ -517,7 +517,7 @@ mutex_sleep(int argc, VALUE *argv, VALUE self)
{
VALUE timeout;
rb_scan_args(argc, argv, "01", &timeout);
timeout = rb_check_arity(argc, 0, 1) ? argv[0] : Qnil;
return rb_mutex_sleep(self, timeout);
}

10
time.c
View file

@ -3764,9 +3764,8 @@ static VALUE
time_localtime_m(int argc, VALUE *argv, VALUE time)
{
VALUE off;
rb_scan_args(argc, argv, "01", &off);
if (!NIL_P(off)) {
if (rb_check_arity(argc, 0, 1) && !NIL_P(off = argv[0])) {
return time_zonelocal(time, off);
}
@ -3881,9 +3880,8 @@ static VALUE
time_getlocaltime(int argc, VALUE *argv, VALUE time)
{
VALUE off;
rb_scan_args(argc, argv, "01", &off);
if (!NIL_P(off)) {
if (rb_check_arity(argc, 0, 1) && !NIL_P(off = argv[0])) {
VALUE zone = off;
if (maybe_tzobj_p(zone)) {
VALUE t = time_dup(time);
@ -4136,9 +4134,7 @@ time_round(int argc, VALUE *argv, VALUE time)
long nd;
struct time_object *tobj;
rb_scan_args(argc, argv, "01", &ndigits);
if (NIL_P(ndigits))
if (!rb_check_arity(argc, 0, 1) || NIL_P(ndigits = argv[0]))
ndigits = INT2FIX(0);
else
ndigits = rb_to_int(ndigits);

View file

@ -4115,10 +4115,9 @@ econv_putback(int argc, VALUE *argv, VALUE self)
int putbackable;
VALUE str, max;
rb_scan_args(argc, argv, "01", &max);
if (NIL_P(max))
if (!rb_check_arity(argc, 0, 1) || NIL_P(max = argv[0])) {
n = rb_econv_putbackable(ec);
}
else {
n = NUM2INT(max);
putbackable = rb_econv_putbackable(ec);

View file

@ -2728,16 +2728,11 @@ rb_const_list(void *data)
VALUE
rb_mod_constants(int argc, const VALUE *argv, VALUE mod)
{
VALUE inherit;
bool inherit = TRUE;
if (argc == 0) {
inherit = Qtrue;
}
else {
rb_scan_args(argc, argv, "01", &inherit);
}
if (rb_check_arity(argc, 0, 1)) inherit = RTEST(argv[0]);
if (RTEST(inherit)) {
if (inherit) {
return rb_const_list(rb_mod_const_of(mod, 0));
}
else {
@ -3294,16 +3289,11 @@ cvar_list(void *data)
VALUE
rb_mod_class_variables(int argc, const VALUE *argv, VALUE mod)
{
VALUE inherit;
bool inherit = TRUE;
st_table *tbl;
if (argc == 0) {
inherit = Qtrue;
}
else {
rb_scan_args(argc, argv, "01", &inherit);
}
if (RTEST(inherit)) {
if (rb_check_arity(argc, 0, 1)) inherit = RTEST(argv[0]);
if (inherit) {
tbl = mod_cvar_of(mod, 0);
}
else {

3
vm.c
View file

@ -422,7 +422,8 @@ vm_stat(int argc, VALUE *argv, VALUE self)
VALUE arg = Qnil;
VALUE hash = Qnil, key = Qnil;
if (rb_scan_args(argc, argv, "01", &arg) == 1) {
if (rb_check_arity(argc, 0, 1) == 1) {
arg = argv[0];
if (SYMBOL_P(arg))
key = arg;
else if (RB_TYPE_P(arg, T_HASH))

View file

@ -1949,16 +1949,9 @@ catch_i(VALUE tag, VALUE data)
*/
static VALUE
rb_f_catch(int argc, VALUE *argv)
rb_f_catch(int argc, VALUE *argv, VALUE self)
{
VALUE tag;
if (argc == 0) {
tag = rb_obj_alloc(rb_cObject);
}
else {
rb_scan_args(argc, argv, "01", &tag);
}
VALUE tag = rb_check_arity(argc, 0, 1) ? argv[0] : rb_obj_alloc(rb_cObject);
return rb_catch_obj(tag, catch_i, 0);
}