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

auto correct bug in Style/SelectByRegexp #10216

Closed
mathieujobin opened this issue Oct 27, 2021 · 1 comment · Fixed by #10218
Closed

auto correct bug in Style/SelectByRegexp #10216

mathieujobin opened this issue Oct 27, 2021 · 1 comment · Fixed by #10218
Labels

Comments

@mathieujobin
Copy link

Getting this failure

 rubocop -A app/controllers/bonuses_controller.rb
Inspecting 1 file
C

Offenses:

app/controllers/bonuses_controller.rb:45:26: C: [Corrected] Style/SelectByRegexp: Prefer grep to select with a regexp match.
              matching = bonuses_on_date.to_a.select { |bonus| bt =~ bonus }
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
app/controllers/bonuses_controller.rb:117:24: C: [Corrected] Style/SelectByRegexp: Prefer grep to select with a regexp match.
            matching = bonuses_on_date.to_a.select { |bonus| bt =~ bonus }
                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

the "corrected version" replace it for

index 6e0c9a9bf3..7f95ffa945 100644
--- a/app/controllers/bonuses_controller.rb
+++ b/app/controllers/bonuses_controller.rb
@@ -42,7 +42,7 @@ class BonusesController < ApplicationController
             if level_template
               level_template.enable_override_for employee
               bonuses_on_date = employee.payments.where(:kind => "bonus", :date => @period.last+1)
-              matching = bonuses_on_date.to_a.select { |bonus| bt =~ bonus }
+              matching = bonuses_on_date.to_a.grep(bonus)
               if matching.length == 0
                 if v == "yes"
                   # create
@@ -114,7 +114,7 @@ protected
           level_template = compensation_level.bonus_template_levels.find_by_bonus_template_id(bt.id)
           if level_template
             level_template.enable_override_for emp
-            matching = bonuses_on_date.to_a.select { |bonus| bt =~ bonus }
+            matching = bonuses_on_date.to_a.grep(bonus)
             if matching.length == 0
               @statuses["#{emp.id}|#{bt.id}"] = "no"
             elsif matching.length == 1

Expected behavior

the var should be bt not bonus

Actual behavior

wrong var is replaced in grep call

Steps to reproduce the problem

see above

RuboCop version

1.22.1 (using Parser 3.0.2.0, rubocop-ast 1.12.0, running on ruby 2.6.8 x86_64-linux)
  - rubocop-rails 2.12.4
@koic
Copy link
Member

koic commented Oct 27, 2021

Thank you for the feedback. I've opened #10218.

koic added a commit to koic/rubocop that referenced this issue Oct 27, 2021
…egexp`

Fix rubocop#10216.

This PR fixes an incorrect autocorrect for `Style/SelectByRegexp`
when using `lvar =~ blockvar` in a block.
bbatsov pushed a commit that referenced this issue Oct 27, 2021
Fix #10216.

This PR fixes an incorrect autocorrect for `Style/SelectByRegexp`
when using `lvar =~ blockvar` in a block.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants