Warning[:strict_unused_block]

to show unused block warning strictly.

```ruby
class C
  def f = nil
end

class D
  def f = yield
end

[C.new, D.new].each{|obj| obj.f{}}
```

In this case, `D#f` accepts a block. However `C#f` doesn't
accept a block. There are some cases passing a block with
`obj.f{}` where `obj` is `C` or `D`. To avoid warnings on
such cases, "unused block warning" will be warned only if
there is not same name which accepts a block.
On the above example, `C.new.f{}` doesn't show any warnings
because there is a same name `D#f` which accepts a block.

We call this default behavior as "relax mode".

`strict_unused_block` new warning category changes from
"relax mode" to "strict mode", we don't check same name
methods and `C.new.f{}` will be warned.

[Feature #15554]
This commit is contained in:
Koichi Sasada 2024-11-06 03:41:59 +09:00
parent 4203c70dfa
commit ab7ab9e450
Notes: git 2024-11-06 02:06:37 +00:00
8 changed files with 20 additions and 14 deletions

4
ruby.c
View file

@ -398,6 +398,7 @@ usage(const char *name, int help, int highlight, int columns)
M("deprecated", "", "Deprecated features."),
M("experimental", "", "Experimental features."),
M("performance", "", "Performance issues."),
M("strict_unused_block", "", "Warning unused block strictly"),
};
#if USE_RJIT
extern const struct ruby_opt_message rb_rjit_option_messages[];
@ -1233,6 +1234,9 @@ proc_W_option(ruby_cmdline_options_t *opt, const char *s, int *warning)
else if (NAME_MATCH_P("performance", s, len)) {
bits = 1U << RB_WARN_CATEGORY_PERFORMANCE;
}
else if (NAME_MATCH_P("strict_unused_block", s, len)) {
bits = 1U << RB_WARN_CATEGORY_STRICT_UNUSED_BLOCK;
}
else {
rb_warn("unknown warning category: '%s'", s);
}