Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix a bug in RSpec/PredicateMatcher #2036

Merged
merged 1 commit into from
Feb 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Master (Unreleased)

- Don't let `RSpec/PredicateMatcher` replace `respond_to?` with two arguments with the RSpec `respond_to` matcher. ([@bquorning])

## 3.4.0 (2025-01-20)

- Fix `RSpec/SortMetadata` cop to limit sorting to trailing metadata arguments. ([@cbliard])
Expand Down
6 changes: 6 additions & 0 deletions lib/rubocop/cop/rspec/predicate_matcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ module InflectedHelper

def check_inflected(node)
predicate_in_actual?(node) do |predicate, to, matcher|
next if cannot_replace_predicate?(predicate)

msg = message_inflected(predicate)
add_offense(node, message: msg) do |corrector|
remove_predicate(corrector, predicate)
Expand All @@ -35,6 +37,10 @@ def check_inflected(node)
$#boolean_matcher? ...)
PATTERN

def cannot_replace_predicate?(send_node)
send_node.method?(:respond_to?) && send_node.arguments.length > 1
end

# @!method be_bool?(node)
def_node_matcher :be_bool?, <<~PATTERN
(send nil? {:be :eq :eql :equal} {true false})
Expand Down
6 changes: 6 additions & 0 deletions spec/rubocop/cop/rspec/predicate_matcher_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@
RUBY
end

it 'accepts respond_to? with a second argument' do
expect_no_offenses(<<~RUBY)
expect(foo.respond_to?(:bar, true)).to be_truthy
RUBY
end

it 'registers an offense for a predicate method with argument' do
expect_offense(<<~RUBY)
expect(foo.something?('foo')).to be_truthy
Expand Down