Add new Style/ReverseFind cop#14750
Merged
bbatsov merged 1 commit intorubocop:masterfrom Jan 15, 2026
Merged
Conversation
Ruby 4.0 introduces `Array#rfind`: https://bugs.ruby-lang.org/issues/21678 This PR adds new `Style/ReverseFind` cop that identifies places where `array.reverse.find` can be replaced by `array.rfind`. ## Safety This cop is unsafe because it cannot be guaranteed that the receiver is an `Array` or responds to the replacement method. ## Example ```ruby # bad array.reverse.find { |item| item.even? } array.reverse.detect { |item| item.even? } array.reverse_each.find { |item| item.even? } array.reverse_each.detect { |item| item.even? } # good array.rfind { |item| item.even? } ``` ## Additional Information Using this approach improves performance as follows: ```ruby require 'benchmark/ips' Benchmark.ips do |x| array = *(1..100) x.report('reverse.find') { array.reverse.find { it == 42 } } x.report('reverse_each.find') { array.reverse_each.find { it == 42 } } x.report('rfind') { array.rfind { it == 42 } } x.compare! end ``` ```console $ ruby rfind.rb ruby 4.0.0 (2025-12-25 revision 553f1675f3) +PRISM [arm64-darwin24] Warming up -------------------------------------- reverse.find 81.778k i/100ms reverse_each.find 59.397k i/100ms rfind 89.719k i/100ms Calculating ------------------------------------- reverse.find 813.856k (± 0.7%) i/s - 4.089M in 5.024325s reverse_each.find 596.506k (± 0.4%) i/s - 3.029M in 5.078403s rfind 889.829k (± 0.5%) i/s - 4.486M in 5.041482s Comparison: rfind: 889828.8 i/s reverse.find: 813855.8 i/s - 1.09x slower reverse_each.find: 596505.7 i/s - 1.49x slower ``` This cop is placed under `Style` rather than `Performance` because it encourages using the dedicated `rfind` method, which is considered a matter of style. As a result, the code becomes shorter and requires fewer method calls, resulting in a simpler style.
ce0bae0 to
c3df5b4
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Ruby 4.0 introduces
Array#rfind:https://bugs.ruby-lang.org/issues/21678
This PR adds new
Style/ReverseFindcop that identifies places wherearray.reverse.findcan be replaced byarray.rfind.Safety
This cop is unsafe because it cannot be guaranteed that the receiver is an
Arrayor responds to the replacement method.Example
Additional Information
Using this approach improves performance as follows:
This cop is placed under
Stylerather thanPerformancebecause it encourages using the dedicatedrfindmethod, which is considered a matter of style. As a result, the code becomes shorter and requires fewer method calls, resulting in a simpler style.Before submitting the PR make sure the following are checked:
[Fix #issue-number](if the related issue exists).master(if not - rebase it).bundle exec rake default. It executes all tests and runs RuboCop on its own code.{change_type}_{change_description}.mdif the new code introduces user-observable changes. See changelog entry format for details.