Skip to content

Commit eecd3e7

Browse files
committed
Add new RSpec/IdenticalEqualityAssertion cop
Closes #1130
1 parent 956f3b5 commit eecd3e7

File tree

7 files changed

+119
-0
lines changed

7 files changed

+119
-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+
* Add new `RSpec/IdenticalEqualityAssertion` cop. ([@tejasbubane][])
6+
57
## 2.2.0 (2021-02-02)
68

79
* Fix `HooksBeforeExamples`, `LeadingSubject`, `LetBeforeExamples` and `ScatteredLet` autocorrection to take into account inline comments and comments immediately before the moved node. ([@Darhazer][])

config/default.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,12 @@ RSpec/HooksBeforeExamples:
349349
VersionAdded: '1.29'
350350
StyleGuide: https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/HooksBeforeExamples
351351

352+
RSpec/IdenticalEqualityAssertion:
353+
Description: Checks for equality assertions with identical expressions on both sides.
354+
Enabled: pending
355+
VersionAdded: '2.3'
356+
StyleGuide: https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/IdenticalEqualityAssertion
357+
352358
RSpec/ImplicitBlockExpectation:
353359
Description: Check that implicit block expectation syntax is not used.
354360
Enabled: true

docs/modules/ROOT/pages/cops.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
* xref:cops_rspec.adoc#rspecfocus[RSpec/Focus]
3636
* xref:cops_rspec.adoc#rspechookargument[RSpec/HookArgument]
3737
* xref:cops_rspec.adoc#rspechooksbeforeexamples[RSpec/HooksBeforeExamples]
38+
* xref:cops_rspec.adoc#rspecidenticalequalityassertion[RSpec/IdenticalEqualityAssertion]
3839
* xref:cops_rspec.adoc#rspecimplicitblockexpectation[RSpec/ImplicitBlockExpectation]
3940
* xref:cops_rspec.adoc#rspecimplicitexpect[RSpec/ImplicitExpect]
4041
* xref:cops_rspec.adoc#rspecimplicitsubject[RSpec/ImplicitSubject]

docs/modules/ROOT/pages/cops_rspec.adoc

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1690,6 +1690,37 @@ end
16901690

16911691
* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/HooksBeforeExamples
16921692

1693+
== RSpec/IdenticalEqualityAssertion
1694+
1695+
|===
1696+
| Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged
1697+
1698+
| Pending
1699+
| Yes
1700+
| No
1701+
| 2.3
1702+
| -
1703+
|===
1704+
1705+
Checks for equality assertions with identical expressions on both sides.
1706+
1707+
=== Examples
1708+
1709+
[source,ruby]
1710+
----
1711+
# bad
1712+
expect(foo.bar).to eq(foo.bar)
1713+
expect(foo.bar).to eql(foo.bar)
1714+
1715+
# good
1716+
expect(foo.bar).to eq(2)
1717+
expect(foo.bar).to eql(2)
1718+
----
1719+
1720+
=== References
1721+
1722+
* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/IdenticalEqualityAssertion
1723+
16931724
== RSpec/ImplicitBlockExpectation
16941725

16951726
|===
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# frozen_string_literal: true
2+
3+
module RuboCop
4+
module Cop
5+
module RSpec
6+
# Checks for equality assertions with identical expressions on both sides.
7+
#
8+
# @example
9+
#
10+
# # bad
11+
# expect(foo.bar).to eq(foo.bar)
12+
# expect(foo.bar).to eql(foo.bar)
13+
#
14+
# # good
15+
# expect(foo.bar).to eq(2)
16+
# expect(foo.bar).to eql(2)
17+
#
18+
class IdenticalEqualityAssertion < Base
19+
MSG = 'Identical equality can never fail.'
20+
21+
def_node_matcher :equality_check?, <<~PATTERN
22+
(send (send nil? :expect $_) :to (send nil? {:eql :eq} $_))
23+
PATTERN
24+
25+
def on_send(node)
26+
equality_check?(node) do |left, right|
27+
return unless left == right
28+
29+
add_offense(node)
30+
end
31+
end
32+
end
33+
end
34+
end
35+
end

lib/rubocop/cop/rspec_cops.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
require_relative 'rspec/focus'
4848
require_relative 'rspec/hook_argument'
4949
require_relative 'rspec/hooks_before_examples'
50+
require_relative 'rspec/identical_equality_assertion'
5051
require_relative 'rspec/implicit_block_expectation'
5152
require_relative 'rspec/implicit_expect'
5253
require_relative 'rspec/implicit_subject'
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# frozen_string_literal: true
2+
3+
RSpec.describe RuboCop::Cop::RSpec::IdenticalEqualityAssertion do
4+
it 'registers an offense when using identical expressions with `eq`' do
5+
expect_offense(<<~RUBY)
6+
expect(foo.bar).to eq(foo.bar)
7+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Identical equality can never fail.
8+
RUBY
9+
end
10+
11+
it 'registers an offense when using identical expressions with `eql`' do
12+
expect_offense(<<~RUBY)
13+
expect(foo.bar.baz).to eql(foo.bar.baz)
14+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Identical equality can never fail.
15+
RUBY
16+
end
17+
18+
it 'registers an offense for trivial constants' do
19+
expect_offense(<<~RUBY)
20+
expect(42).to eq(42)
21+
^^^^^^^^^^^^^^^^^^^^ Identical equality can never fail.
22+
RUBY
23+
end
24+
25+
it 'registers an offense for complex constants' do
26+
expect_offense(<<~RUBY)
27+
expect({a: 1, b: :b}).to eql({a: 1, b: :b})
28+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Identical equality can never fail.
29+
RUBY
30+
end
31+
32+
it 'does not register offense for different expressions' do
33+
expect_no_offenses(<<~RUBY)
34+
expect(foo.bar).to eq(bar.foo)
35+
RUBY
36+
end
37+
38+
it 'checks for whole expression' do
39+
expect_no_offenses(<<~RUBY)
40+
expect(Foo.new(1).foo).to eql(Foo.new(2).bar)
41+
RUBY
42+
end
43+
end

0 commit comments

Comments
 (0)