Skip to content

Commit ae4aadd

Browse files
committed
Fix a bug in RSpec/PredicateMatcher
Don't let `RSpec/PredicateMatcher` replace `respond_to?` with two arguments with the RSpec `respond_to` matcher, since the 2nd argument has a different meaning in the two methods. Fixes #2010
1 parent 226f332 commit ae4aadd

File tree

3 files changed

+14
-0
lines changed

3 files changed

+14
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## Master (Unreleased)
44

5+
- Don't let `RSpec/PredicateMatcher` replace `respond_to?` with two arguments with the RSpec `respond_to` matcher. ([@bquorning])
6+
57
## 3.4.0 (2025-01-20)
68

79
- Fix `RSpec/SortMetadata` cop to limit sorting to trailing metadata arguments. ([@cbliard])

lib/rubocop/cop/rspec/predicate_matcher.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ module InflectedHelper
1515

1616
def check_inflected(node)
1717
predicate_in_actual?(node) do |predicate, to, matcher|
18+
next if cannot_replace_predicate?(predicate)
19+
1820
msg = message_inflected(predicate)
1921
add_offense(node, message: msg) do |corrector|
2022
remove_predicate(corrector, predicate)
@@ -35,6 +37,10 @@ def check_inflected(node)
3537
$#boolean_matcher? ...)
3638
PATTERN
3739

40+
def cannot_replace_predicate?(send_node)
41+
send_node.method?(:respond_to?) && send_node.arguments.length > 1
42+
end
43+
3844
# @!method be_bool?(node)
3945
def_node_matcher :be_bool?, <<~PATTERN
4046
(send nil? {:be :eq :eql :equal} {true false})

spec/rubocop/cop/rspec/predicate_matcher_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,12 @@
7373
RUBY
7474
end
7575

76+
it 'accepts respond_to? with a second argument' do
77+
expect_no_offenses(<<~RUBY)
78+
expect(foo.respond_to?(:bar, true)).to be_truthy
79+
RUBY
80+
end
81+
7682
it 'registers an offense for a predicate method with argument' do
7783
expect_offense(<<~RUBY)
7884
expect(foo.something?('foo')).to be_truthy

0 commit comments

Comments
 (0)