rubocop-rails
rubocop-rails copied to clipboard
Add cop for checking assingments to `ignored_columns`
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.
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
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.
figure out which one is better
I'd go with +=
, keeping in mind inheritance and mixins.
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 +=
.
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. 👍
Thank you for the opinions! Let us move on with this cop.
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.
Thanks!