mruby-kernel-ext: add error helper function to eliminate goto

Add arg_error() helper function to replace goto statements used for
error handling. This function is marked with mrb_noreturn attribute
since it calls mrb_raise which never returns.

Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
Yukihiro "Matz" Matsumoto
2025-11-11 11:23:14 +09:00
parent e0393943f1
commit 4795b7e3ca
+10 -5
View File
@@ -149,6 +149,12 @@ mrb_f_callee(mrb_state *mrb, mrb_value self)
* Integer("111", 2) #=> 7
* Integer(nil) #=> TypeError
*/
static mrb_noreturn void
arg_error(mrb_state *mrb)
{
mrb_raise(mrb, E_ARGUMENT_ERROR, "base specified for non string value");
}
static mrb_value
mrb_f_integer(mrb_state *mrb, mrb_value self)
{
@@ -157,18 +163,18 @@ mrb_f_integer(mrb_state *mrb, mrb_value self)
mrb_get_args(mrb, "o|i", &val, &base);
if (mrb_nil_p(val)) {
if (base != 0) goto arg_error;
if (base != 0) arg_error(mrb);
mrb_raise(mrb, E_TYPE_ERROR, "can't convert nil into Integer");
}
switch (mrb_type(val)) {
#ifndef MRB_NO_FLOAT
case MRB_TT_FLOAT:
if (base != 0) goto arg_error;
if (base != 0) arg_error(mrb);
return mrb_float_to_integer(mrb, val);
#endif
case MRB_TT_INTEGER:
if (base != 0) goto arg_error;
if (base != 0) arg_error(mrb);
return val;
case MRB_TT_STRING:
@@ -184,8 +190,7 @@ mrb_f_integer(mrb_state *mrb, mrb_value self)
val = tmp;
goto string_conv;
}
arg_error:
mrb_raise(mrb, E_ARGUMENT_ERROR, "base specified for non string value");
arg_error(mrb);
}
/* to raise TypeError */
return mrb_ensure_integer_type(mrb, val);