Skip to content

Commit

Permalink
Merge pull request #321 from koic/fix_false_positive_for_rails_inquiry
Browse files Browse the repository at this point in the history
[Fix #319] Fix a false positive for `Rails/Inquiry`
  • Loading branch information
koic authored Aug 9, 2020
2 parents dc888a3 + a36dc5b commit 6517653
Show file tree
Hide file tree
Showing 3 changed files with 27 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
23 changes: 21 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,35 @@
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` with no receiver' do
expect_no_offenses(<<~RUBY)
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 6517653

Please sign in to comment.