diff --git a/changelog/fix_error_for_rails_not_null_column.md b/changelog/fix_error_for_rails_not_null_column.md new file mode 100644 index 0000000000..0a5813fb15 --- /dev/null +++ b/changelog/fix_error_for_rails_not_null_column.md @@ -0,0 +1 @@ +* [#1299](https://github.com/rubocop/rubocop-rails/pull/1299): Fix an error for `Rails/NotNullColumn` when the block for `change_table` is empty. ([@earlopain][]) diff --git a/lib/rubocop/cop/rails/not_null_column.rb b/lib/rubocop/cop/rails/not_null_column.rb index 5d6bc67c11..3395e41265 100644 --- a/lib/rubocop/cop/rails/not_null_column.rb +++ b/lib/rubocop/cop/rails/not_null_column.rb @@ -136,6 +136,8 @@ def check_add_reference_in_change_table(node, table) def check_change_table(node) change_table?(node) do |table| + next unless node.body + children = node.body.begin_type? ? node.body.children : [node.body] children.each do |child| check_add_column_in_change_table(child, table) diff --git a/spec/rubocop/cop/rails/not_null_column_spec.rb b/spec/rubocop/cop/rails/not_null_column_spec.rb index a5c9b3dda2..580f998026 100644 --- a/spec/rubocop/cop/rails/not_null_column_spec.rb +++ b/spec/rubocop/cop/rails/not_null_column_spec.rb @@ -85,6 +85,17 @@ def change end RUBY end + + it 'does not register an offense when the block is empty' do + expect_no_offenses(<<~RUBY) + class ExampleMigration < ActiveRecord::Migration[7.0] + def change + change_table :invoices do |t| + end + end + end + RUBY + end end context 'with change_table call' do