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

Rails/Pluck has false positive and introduced incorrect code when autocorrect is run #839

Closed
PatrickLerner opened this issue Oct 26, 2022 · 2 comments

Comments

@PatrickLerner
Copy link

Updating from 2.17.0 to 2.17.1 leads to an incorrect error and incorrect auto-fix.

CI error:

Offenses:
blah.rb:44:31: C: [Correctable] Rails/Pluck: Prefer pluck(key) over map { |key| row[key] }.
          exporter << columns.map { |key| row[key] }
                              ^^^^^^^^^^^^^^^^^^^^^^

Autofix result:

diff --git a/blah.rb b/blah.rb
index 7347dfd3f9..855a2d63c7 100644
--- a/blah.rb
+++ b/blah.rb
@@ -40,8 +40,8 @@ module Backoffice
       Exporter.generate do |exporter|
         exporter << columns.map { |key| self.class.csv_columns[key] }
 
-        data.each do |row|
-          exporter << columns.map { |key| row[key] }
+        data.each do |_row|
+          exporter << columns.pluck(key)
         end
       end
     end

Expected behavior

It seems like pluck is not applicable here at all since key is dynamic

Actual behavior

rubocop auto-fixes code that fails to work

RuboCop version

$  rubocop -V
1.37.1 (using Parser 3.1.2.1, rubocop-ast 1.23.0, running on ruby 2.7.2) [arm64-darwin21]
  - rubocop-graphql 0.15.1
  - rubocop-rails 2.17.1
  - rubocop-rake 0.6.0
  - rubocop-rspec 2.13.2
@PatrickLerner
Copy link
Author

Ah, I suspect it is just a duplicate of #833 and a fix is already merged.

@PatrickLerner
Copy link
Author

I tested it against main branch here, and it works as expected. We will wait for next release then and will not upgrade to the buggy x.1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant