-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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
RedundantMerge breaks ActiveRecord objects #2781
Comments
I'm seeing this too. Justin's description is good, but here's an example from my codebase, in case it helps. Source:
Output:
|
Same here: # Rubocop ➙ Performance/RedundantMerge:
# Use origin_account[:amount_cents] = origin_account.amount_cents - amount_cents
# instead of origin_account.update amount_cents: origin_account.amount_cents - amount_cents
# My code:
origin_account.update(amount_cents: origin_account.amount_cents - amount_cents) |
Yes. I brought this up in the issue when I made the PR. It's a thorny path, and I've been thinking a lot about it since then. It is a Ruby static code analyzer, after all, but the question of how many exceptions should be made on account of popular frameworks might not be that straight forward. Since we already have Rails specific cops, maybe we can have some way of marking a cop as "Rails unsafe", or, though more complicated, use an alternate cop when the Rails flag i used? |
The Rails-specific cops will be removed from RuboCop at some point as they don't really belong there. This was a silly mistake I made when I had way more time to work on the project and my ambitions for it were bigger. Cops in general should probably have some "safety" level or something - we're quite aware which cops can produce false positives and cause problems and which can't. This has been proposed several times so far, but we've yet to make any progress in this direction. |
👍 to remove this bad change for Rails Project ! |
The trouble is that seeing a call to This whole process would likely slow RC down quite a bit, and would be unable to conclude anything about code which uses metaprogramming techniques. This would be a whole new direction for RC, quite distinct from the mostly "coding style"/syntactic checks which it currently performs. |
It would also be entirely impossible to trace the type of objects returned from calls into an API external to the codebase being linted. |
Once 108acca is released, should we give this cop ( |
It's released. |
I can confirm that rubocop 0.37.1, with |
Thanks @bbatsov, the version 0.37.1 works fine for our Rails project 👍 |
**Why**: The current version used by Hound (0.37.0) contains a bug with the RedundantMerge cop that causes a false positive when calling `update` on ActiveRecord objects. See rubocop/rubocop#2781 **How**: Update to 0.37.1 or higher, which fixes the issue.
**Why**: The current version used by Hound (0.37.0) contains a bug with the RedundantMerge cop that causes a false positive when calling `update` on ActiveRecord objects. See rubocop/rubocop#2781 **How**: Update to 0.37.1 or higher, which fixes the issue.
In this case the record object is an instance of an ActiveRecord object. It is not a hash.
ActiveRecord::Base#update
changes the object in memory and then persists the object, theActiveRecord::Base#[]=
only changes the object in memoryThe text was updated successfully, but these errors were encountered: