rubocop-rails icon indicating copy to clipboard operation
rubocop-rails copied to clipboard

Add cop for checking assingments to `ignored_columns`

Open kkitadate opened this issue 2 years ago • 3 comments

This PR is a rebase of https://github.com/rubocop/rubocop-rails/pull/514 on rubocop:master. It is requested by https://github.com/rubocop/rubocop-rails/pull/514#issuecomment-1243272703.


Before submitting the PR make sure the following are checked:

  • [x] The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • [x] Wrote good commit messages.
  • [x] Commit message starts with [Fix #issue-number] (if the related issue exists).
  • [x] Feature branch is up-to-date with master (if not - rebase it).
  • [x] Squashed related commits together.
  • [x] Added tests.
  • [x] Ran bundle exec rake default. It executes all tests and runs RuboCop on its own code.
  • [x] Added an entry (file) to the changelog folder named {change_type}_{change_description}.md if the new code introduces user-observable changes. See changelog entry format for details.
  • [x] If this is a new cop, consider making a corresponding update to the Rails Style Guide.

kkitadate avatar Sep 12 '22 07:09 kkitadate

The issue to be resolved conflicts with #761. We might need some time to figure out which one is better. Anyway, the next release is a bug fix, so we have time. cc @sikachu

koic avatar Sep 12 '22 07:09 koic

Oh, interesting. So this approach is to suggest that we always do += instead of = altogether.

I think this approach definitely makes the cop super clean, as you just detect for = instead of +=.

However, I find that it is a bit weird since if you look at just the model itself, you'll have to use += even for the first call. You have to have background knowledge of "Rails sets this variable to [], so that's why we can just use +=.

In our codebase, we never use += but we did have a few accidents of calling = a few times. Hence, I'm in favor of the = version since it's more clear without the need of the background knowledge.

sikachu avatar Sep 12 '22 08:09 sikachu

figure out which one is better

I'd go with +=, keeping in mind inheritance and mixins.

pirj avatar Sep 12 '22 09:09 pirj

For reference, #761 also started as a cop checking for =. However, due to the impossibility of checking for mixins and inheritance, this cop might not flag invalid behavior, while it is always correct to use +=.

fsateler avatar Sep 26 '22 19:09 fsateler

Yeah, I'm retracting my #761. Let's use += since it works better for inheritance and it makes it much easier to auto-fix it. 👍

sikachu avatar Sep 27 '22 06:09 sikachu

Thank you for the opinions! Let us move on with this cop.

koic avatar Sep 27 '22 18:09 koic

CI seems to have failed because rubocop/rubocop#11002 was merged 5 hours ago. https://github.com/rubocop/rubocop-rails/pull/798 will fix this failure.

kkitadate avatar Sep 29 '22 10:09 kkitadate

Thanks!

koic avatar Oct 02 '22 08:10 koic