Skip to content

Commit

Permalink
[Fix rubocop#319] Fix a false positive for Rails/Inquiry
Browse files Browse the repository at this point in the history
Fixes rubocop#319.

This PR fixes a false positive for `Rails/Inquiry` when
`#inquiry`'s receiver is a variable.
It should only detect when receiver is an array literal and
a string literal.
  • Loading branch information
koic committed Aug 9, 2020
1 parent dc888a3 commit fbcda86
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

* [#315](https://github.com/rubocop-hq/rubocop-rails/pull/315): Allow to use frozen scope for `Rails/UniqueValidationWithoutIndex`. ([@krim][])
* [#313](https://github.com/rubocop-hq/rubocop-rails/pull/313): Fix `Rails/ActiveRecordCallbacksOrder` to preserve the original callback execution order. ([@eugeneius][])
* [#319](https://github.com/rubocop-hq/rubocop-rails/issues/319): Fix a false positive for `Rails/Inquiry` when `#inquiry`'s receiver is a variable. ([@koic][])

### Changes

Expand Down
6 changes: 5 additions & 1 deletion lib/rubocop/cop/rails/inquiry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ class Inquiry < Cop
MSG = "Prefer Ruby's comparison operators over Active Support's `inquiry`."

def on_send(node)
add_offense(node, location: :selector) if node.method?(:inquiry) && node.arguments.empty?
return unless node.method?(:inquiry) && node.arguments.empty?
return unless (receiver = node.receiver)
return if !receiver.str_type? && !receiver.array_type?

add_offense(node, location: :selector)
end
end
end
Expand Down
17 changes: 15 additions & 2 deletions spec/rubocop/cop/rails/inquiry_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,29 @@
RSpec.describe RuboCop::Cop::Rails::Inquiry do
subject(:cop) { described_class.new }

it 'registers an offense when using `#inquiry`' do
it 'registers an offense when using `String#inquiry`' do
expect_offense(<<~RUBY)
'two'.inquiry
^^^^^^^ Prefer Ruby's comparison operators over Active Support's `inquiry`.
RUBY
end

it 'registers an offense when using `Array#inquiry`' do
expect_offense(<<~RUBY)
[foo, bar].inquiry
^^^^^^^ Prefer Ruby's comparison operators over Active Support's `inquiry`.
RUBY
end

it "does not register an offense when `#inquiry`'s receiver is a variable" do
expect_no_offenses(<<~RUBY)
foo.inquiry
RUBY
end

it 'does not register an offense when using `#inquiry` with arguments' do
expect_no_offenses(<<~RUBY)
foo.inquiry(bar)
'foo'.inquiry(bar)
RUBY
end
end

0 comments on commit fbcda86

Please sign in to comment.