Skip to content

Commit

Permalink
Fix an error for Lint/UselessRuby2Keywords
Browse files Browse the repository at this point in the history
This PR fixes the following error for `Lint/UselessRuby2Keywords`
when using `Proc#ruby2_keywords`.

```ruby
block = proc { |_, *args| klass.new(*args) }
block.ruby2_keywords if block.respond_to?(:ruby2_keywords
```

```console
% bundle exec rubocop --only Lint/UselessRuby2Keywords -d
(snip)

An error occurred while Lint/UselessRuby2Keywords cop was inspecting /Users/koic/src/github.com/koic/rubocop-issues/ruby2_keywords/example.rb:2:0.
undefined method `def_type?' for nil:NilClass
/Users/koic/src/github.com/rubocop/rubocop/lib/rubocop/cop/lint/useless_ruby2_keywords.rb:80:in `on_send'
```
  • Loading branch information
koic authored and bbatsov committed Jan 1, 2023
1 parent 3cce8c8 commit 33bf9a0
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
1 change: 1 addition & 0 deletions 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][])
8 changes: 5 additions & 3 deletions lib/rubocop/cop/lint/useless_ruby2_keywords.rb
Expand Up @@ -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

Expand Down
7 changes: 7 additions & 0 deletions spec/rubocop/cop/lint/useless_ruby2_keywords_spec.rb
Expand Up @@ -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

0 comments on commit 33bf9a0

Please sign in to comment.