Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

False positive Performance/RedundantEqualityComparisonBlock when pattern depends on the enumerable's element #255

Closed
movermeyer opened this issue Aug 9, 2021 · 0 comments · Fixed by #256
Labels
bug Something isn't working

Comments

@movermeyer
Copy link

Steps to reproduce the problem

I have a piece of code that is trying to filter a list of key value pairs according to whether the key has a corresponding prefix in the allow list:

# frozen_string_literal: true

# These are path prefixes that we allow
# anything KVPs without a key that has one of these as a prefix will be removed
PATH_PREFIXES_ALLOW_LIST = [["a", "b", "c"], ["d", "e"]]

input = [
  [["a", "b", "c"], 1],
  [["x", "y", "z"], 2],
  [["d", "e", "f"], 3],
  [["d", "e", "g"], 4],
]

filtered = input.select { |key, _value| PATH_PREFIXES_ALLOW_LIST.any? { |path| path == key[0...path.size] } }

expected = [
  [["a", "b", "c"], 1],
  [["d", "e", "f"], 3],
  [["d", "e", "g"], 4],
]

puts(filtered == expected)

Actual behavior

This code works. However, RuboCop complains:

Inspecting 1 file
C

Offenses:

false_positive.rb:14:66: C: [Correctable] Performance/RedundantEqualityComparisonBlock: Use any?(key[0...path.size]) instead of block.
filtered = input.select { |key, _value| PATH_PREFIXES_ALLOW_LIST.any? { |path| path == key[0...path.size] } }
                                                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

1 file inspected, 1 offense detected, 1 offense auto-correctable

Note that making the suggested change would break the code:

  • Without the reference to path, key[0...path.size] does not work.

i.e. the value of the pattern in any? to use depends on the length of the path being checked, so it cannot be passed in as suggested by the check???

Expected behavior

I would expect that RuboCop either doesn't complain, or suggests a fix that has the same behaviour as the existing code,

RuboCop version

$ bundle exec rubocop -V
1.18.3 (using Parser 3.0.2.0, rubocop-ast 1.8.0, running on ruby 2.7.1 x86_64-darwin19)
  - rubocop-performance 1.11.4
@koic koic added the bug Something isn't working label Aug 9, 2021
koic added a commit to koic/rubocop-performance that referenced this issue Aug 10, 2021
…mparisonBlock`

Fixes rubocop#255.

This PR fixes a false positive for `Style/RedundantEqualityComparisonBlock`
when using block argument is used for an argument of operand.
koic added a commit to koic/rubocop-performance that referenced this issue Aug 10, 2021
…mparisonBlock`

Fixes rubocop#255.

This PR fixes a false positive for `Style/RedundantEqualityComparisonBlock`
when using block argument is used for an argument of operand.
koic added a commit to koic/rubocop-performance that referenced this issue Aug 10, 2021
koic added a commit to koic/rubocop-performance that referenced this issue Aug 10, 2021
koic added a commit to koic/rubocop-performance that referenced this issue Aug 10, 2021
…mparisonBlock`

Fixes rubocop#255.

This PR fixes a false positive for `Style/RedundantEqualityComparisonBlock`
when using block argument is used for an argument of operand.
koic added a commit to koic/rubocop-performance that referenced this issue Aug 10, 2021
…lityComparisonBlock`

Fixes rubocop#255.

This PR fixes a false positive for `Performance/RedundantEqualityComparisonBlock`
when using block argument is used for an argument of operand.
@koic koic closed this as completed in #256 Aug 14, 2021
koic added a commit that referenced this issue Aug 14, 2021
…ant_equality_comparison_block

[Fix #255] Fix a false positive for `Performance/RedundantEqualityComparisonBlock`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
2 participants