diff --git a/CHANGELOG.md b/CHANGELOG.md index 3545855977..d491654cb6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/rubocop/cop/rails/inquiry.rb b/lib/rubocop/cop/rails/inquiry.rb index 9acefe3f59..f5ea160187 100644 --- a/lib/rubocop/cop/rails/inquiry.rb +++ b/lib/rubocop/cop/rails/inquiry.rb @@ -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 diff --git a/spec/rubocop/cop/rails/inquiry_spec.rb b/spec/rubocop/cop/rails/inquiry_spec.rb index 54def5ad9e..d6cfa6de3f 100644 --- a/spec/rubocop/cop/rails/inquiry_spec.rb +++ b/spec/rubocop/cop/rails/inquiry_spec.rb @@ -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