Skip to content

Add cop to prevent using block with scan_each variants

Sylvester Chin requested to merge sc1-rubocop-scaneach-no-block into master

What does this MR do and why?

This MR adds a custom cop to disallow the use of scan_each/sscan_each/hscan_each/zscan_each with a block. Those methods are implemented with to_enum and returns an Enumerator if no blocks are given.

If a block is given, the result of the command is nil whereas if an Enumerator is returned, .first can be performed to cheaply check for cache misses. This is relevant in MultiStore since passing a block will always lead to a fallback read.

For more background, see #386745 (closed) for more details.

All 4 methods we enforce this cop on are verified to to_enum:

➜  redis-rb git:(v4.8.0) rg to_enum
lib/redis/commands/keys.rb
50:        return to_enum(:scan_each, **options) unless block_given?

lib/redis/commands/sets.rb
212:        return to_enum(:sscan_each, key, **options) unless block_given?

lib/redis/commands/hashes.rb
240:        return to_enum(:hscan_each, key, **options) unless block_given?

lib/redis/commands/sorted_sets.rb
768:        return to_enum(:zscan_each, key, **options) unless block_given?

Screenshots or screen recordings

Screenshots are required for UI changes, and strongly recommended for all other merge requests.

How to set up and validate locally

Numbered steps to set up and validate the change are strongly suggested.

MR acceptance checklist

This checklist encourages us to confirm any changes have been analyzed to reduce risks in quality, performance, reliability, security, and maintainability.

Edited by Sylvester Chin

Merge request reports