Skip to content

Commit e84141a

Browse files
committed
Merge pull request #1554 from ydah/fix-false-positive-rspec-PredicateMatcher
Fix a false positive for `RSpec/PredicateMatcher` when multiple arguments and not replaceable method
1 parent 9e8c975 commit e84141a

File tree

3 files changed

+41
-15
lines changed

3 files changed

+41
-15
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
- Fix a false positive for `RSpec/NoExpectationExample` when using skipped in metadata is multiline string. ([@ydah])
77
- Fix a false positive for `RSpec/ContextMethod` when multi-line context with `#` at the beginning. ([@ydah])
88
- Fix an incorrect autocorrect for `RSpec/PredicateMatcher` when multiline expect and predicate method with heredoc. ([@ydah])
9+
- Fix a false positive for `RSpec/PredicateMatcher` when `include` with multiple argument. ([@ydah])
910

1011
## 2.17.0 (2023-01-13)
1112

lib/rubocop/cop/rspec/predicate_matcher.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,8 @@ def check_explicit(node) # rubocop:disable Metrics/MethodLength
149149
return if part_of_ignored_node?(node)
150150

151151
predicate_matcher?(node) do |actual, matcher|
152+
next unless replaceable_matcher?(matcher)
153+
152154
add_offense(node, message: message_explicit(matcher)) do |corrector|
153155
next if uncorrectable_matcher?(node, matcher)
154156

@@ -157,6 +159,15 @@ def check_explicit(node) # rubocop:disable Metrics/MethodLength
157159
end
158160
end
159161

162+
def replaceable_matcher?(matcher)
163+
case matcher.method_name.to_s
164+
when 'include'
165+
matcher.arguments.one?
166+
else
167+
true
168+
end
169+
end
170+
160171
def uncorrectable_matcher?(node, matcher)
161172
heredoc_argument?(matcher) && !same_line?(node, matcher)
162173
end

spec/rubocop/cop/rspec/predicate_matcher_spec.rb

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,14 @@
9595

9696
it 'registers an offense for a predicate method with heredoc' do
9797
expect_offense(<<~RUBY)
98-
expect(foo.include?(<<~TEXT)).to be_truthy
99-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `include` matcher over `include?`.
98+
expect(foo.something?(<<~TEXT)).to be_truthy
99+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `be_something` matcher over `something?`.
100100
bar
101101
TEXT
102102
RUBY
103103

104104
expect_correction(<<~RUBY)
105-
expect(foo).to include(<<~TEXT)
105+
expect(foo).to be_something(<<~TEXT)
106106
bar
107107
TEXT
108108
RUBY
@@ -346,14 +346,14 @@
346346
'heredoc and multiline expect' do
347347
expect_offense(<<~RUBY)
348348
expect(foo)
349-
^^^^^^^^^^^ Prefer using `include?` over `include` matcher.
350-
.to include(<<~TEXT)
349+
^^^^^^^^^^^ Prefer using `something?` over `be_something` matcher.
350+
.to be_something(<<~TEXT)
351351
bar
352352
TEXT
353353
354354
expect(foo)
355-
^^^^^^^^^^^ Prefer using `include?` over `include` matcher.
356-
.to include(bar, <<~TEXT, 'baz')
355+
^^^^^^^^^^^ Prefer using `something?` over `be_something` matcher.
356+
.to be_something(bar, <<~TEXT, 'baz')
357357
bar
358358
TEXT
359359
RUBY
@@ -365,14 +365,14 @@
365365
'heredoc include #{} and multiline expect' do
366366
expect_offense(<<~'RUBY')
367367
expect(foo)
368-
^^^^^^^^^^^ Prefer using `include?` over `include` matcher.
369-
.to include(<<~TEXT)
368+
^^^^^^^^^^^ Prefer using `something?` over `be_something` matcher.
369+
.to be_something(<<~TEXT)
370370
#{bar}
371371
TEXT
372372
373373
expect(foo)
374-
^^^^^^^^^^^ Prefer using `include?` over `include` matcher.
375-
.to include(bar, <<~TEXT, 'baz')
374+
^^^^^^^^^^^ Prefer using `something?` over `be_something` matcher.
375+
.to be_something(bar, <<~TEXT, 'baz')
376376
#{bar}
377377
TEXT
378378
RUBY
@@ -384,21 +384,35 @@
384384
'heredoc surrounded by back ticks and multiline expect' do
385385
expect_offense(<<~'RUBY')
386386
expect(foo)
387-
^^^^^^^^^^^ Prefer using `include?` over `include` matcher.
388-
.to include(<<~`COMMAND`)
387+
^^^^^^^^^^^ Prefer using `something?` over `be_something` matcher.
388+
.to be_something(<<~`COMMAND`)
389389
pwd
390390
COMMAND
391391
392392
expect(foo)
393-
^^^^^^^^^^^ Prefer using `include?` over `include` matcher.
394-
.to include(bar, <<~COMMAND, 'baz')
393+
^^^^^^^^^^^ Prefer using `something?` over `be_something` matcher.
394+
.to be_something(bar, <<~COMMAND, 'baz')
395395
pwd
396396
COMMAND
397397
RUBY
398398

399399
expect_no_corrections
400400
end
401401

402+
it 'does not register an offense for a `include` ' \
403+
'with no argument' do
404+
expect_no_offenses(<<~RUBY)
405+
expect(foo).to include
406+
RUBY
407+
end
408+
409+
it 'does not register an offense for a `include` ' \
410+
'with multiple arguments' do
411+
expect_no_offenses(<<~RUBY)
412+
expect(foo).to include(foo, bar)
413+
RUBY
414+
end
415+
402416
it 'registers an offense for a predicate method with a block' do
403417
expect_offense(<<~RUBY)
404418
expect(foo).to be_all { |x| x.present? }

0 commit comments

Comments
 (0)