diff --git a/changelog/fix_an_error_for_lint_useless_ruby2_keywords.md b/changelog/fix_an_error_for_lint_useless_ruby2_keywords.md new file mode 100644 index 00000000000..ee6893f27ff --- /dev/null +++ b/changelog/fix_an_error_for_lint_useless_ruby2_keywords.md @@ -0,0 +1 @@ +* [#11369](https://github.com/rubocop/rubocop/pull/11369): Fix an error for `Lint/UselessRuby2Keywords` when using `Proc#ruby2_keywords`. ([@koic][]) diff --git a/lib/rubocop/cop/lint/useless_ruby2_keywords.rb b/lib/rubocop/cop/lint/useless_ruby2_keywords.rb index e1ee798d41e..b7d93599133 100644 --- a/lib/rubocop/cop/lint/useless_ruby2_keywords.rb +++ b/lib/rubocop/cop/lint/useless_ruby2_keywords.rb @@ -77,10 +77,12 @@ class UselessRuby2Keywords < Base PATTERN def on_send(node) - if node.first_argument.def_type? - inspect_def(node, node.first_argument) + return unless (first_argument = node.first_argument) + + if first_argument.def_type? + inspect_def(node, first_argument) elsif node.first_argument.sym_type? - inspect_sym(node, node.first_argument) + inspect_sym(node, first_argument) end end diff --git a/spec/rubocop/cop/lint/useless_ruby2_keywords_spec.rb b/spec/rubocop/cop/lint/useless_ruby2_keywords_spec.rb index 536564b532e..2e303ef0189 100644 --- a/spec/rubocop/cop/lint/useless_ruby2_keywords_spec.rb +++ b/spec/rubocop/cop/lint/useless_ruby2_keywords_spec.rb @@ -163,5 +163,12 @@ def foo(**kwargs) ^^^^^^^^^^^^^^^^^^^ `ruby2_keywords` is unnecessary for method `foo`. RUBY end + + it 'does not register an offense for `Proc#ruby2_keywords`' do + expect_no_offenses(<<~RUBY) + block = proc { |_, *args| klass.new(*args) } + block.ruby2_keywords if block.respond_to?(:ruby2_keywords) + RUBY + end end end