Skip to content

Commit 9ba6c2a

Browse files
committed
Fix UnusedIgnoredColumns detection when using +=
1 parent b930670 commit 9ba6c2a

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#895](https://github.com/rubocop/rubocop-rails/pull/895): Fix `Rails/UnusedIgnoredColumns` not recognizing columns added via `+=`. ([@lucthev][])

lib/rubocop/cop/rails/unused_ignored_columns.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,16 @@ class UnusedIgnoredColumns < Base
2828
(send self :ignored_columns= $array)
2929
PATTERN
3030

31+
def_node_matcher :appended_ignored_columns, <<~PATTERN
32+
(op-asgn (send self :ignored_columns) :+ $array)
33+
PATTERN
34+
3135
def_node_matcher :column_name, <<~PATTERN
3236
({str sym} $_)
3337
PATTERN
3438

3539
def on_send(node)
36-
return unless (columns = ignored_columns(node))
40+
return unless (columns = ignored_columns(node) || appended_ignored_columns(node))
3741
return unless schema
3842

3943
table = table(node)
@@ -43,6 +47,7 @@ def on_send(node)
4347
check_column_existence(column_node, table)
4448
end
4549
end
50+
alias on_op_asgn on_send
4651

4752
private
4853

spec/rubocop/cop/rails/unused_ignored_columns_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,17 @@ class User < ApplicationRecord
105105
RUBY
106106
end
107107
end
108+
109+
context 'when using addition assignment on ignored_columns' do
110+
it 'registers an offense to the nonexistent column' do
111+
expect_offense(<<~RUBY)
112+
class User < ApplicationRecord
113+
self.ignored_columns += ['real_name']
114+
^^^^^^^^^^^ Remove `real_name` from `ignored_columns` because the column does not exist.
115+
end
116+
RUBY
117+
end
118+
end
108119
end
109120

110121
context 'with no tables db/schema.rb' do

0 commit comments

Comments
 (0)