Skip to content

Set#initialize does not warn about unused block #41

@headius

Description

@headius

Set#initialize has two forms:

  • No arguments, which just creates an empty Set.
  • One Enumerable argument, used to populate the new Set.

If a block is passed in, it is only used along the one-argument path. The zero-argument path ignores it and does not warn.

This led to a behavior difference in JRuby's Set, which does warn if no arguments are passed and a block is given: jruby/jruby#8728 (comment)

[] jruby94 $ ruby -v -rset -e 'p Set.new {}'
jruby 9.4.13.0-SNAPSHOT (3.1.4) 2025-03-31 e0de6e19d3 OpenJDK 64-Bit Server VM 21.0.6+7-LTS on 21.0.6+7-LTS +jit [arm64-darwin]
-e:1: warning: given block not used
#<Set: {}>
[] jruby94 $ ruby -v -rset -e 'p Set.new([]) {}'
jruby 9.4.13.0-SNAPSHOT (3.1.4) 2025-03-31 e0de6e19d3 OpenJDK 64-Bit Server VM 21.0.6+7-LTS on 21.0.6+7-LTS +jit [arm64-darwin]
#<Set: {}>

With CRuby moving toward emitting warnings when passed blocks are unused, I believe it would be more consistent to emit a warning in set.rb when a block passed to initialize won't be used.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions