diff --git a/changelog/fix_an_error_for_rails_env.md b/changelog/fix_an_error_for_rails_env.md new file mode 100644 index 0000000000..ffe69c47c4 --- /dev/null +++ b/changelog/fix_an_error_for_rails_env.md @@ -0,0 +1 @@ +* [#1556](https://github.com/rubocop/rubocop-rails/pull/1556): Fix an error for `Rails/Env` when assigning `Rails.env`. ([@koic][]) diff --git a/lib/rubocop/cop/rails/env.rb b/lib/rubocop/cop/rails/env.rb index bfc4ebe2b7..b5850dd7c0 100644 --- a/lib/rubocop/cop/rails/env.rb +++ b/lib/rubocop/cop/rails/env.rb @@ -45,7 +45,7 @@ def on_send(node) return unless node.receiver&.const_name == 'Rails' parent = node.parent - return unless parent&.predicate_method? + return unless parent.respond_to?(:predicate_method?) && parent.predicate_method? return if ALLOWED_LIST.include?(parent.method_name) diff --git a/spec/rubocop/cop/rails/env_spec.rb b/spec/rubocop/cop/rails/env_spec.rb index 69628ea9cb..77d2680cdb 100644 --- a/spec/rubocop/cop/rails/env_spec.rb +++ b/spec/rubocop/cop/rails/env_spec.rb @@ -22,6 +22,12 @@ RUBY end + it 'does not register an offense when assigning `Rails.env`' do + expect_no_offenses(<<~RUBY) + rails_env = Rails.env + RUBY + end + it 'does not register an offense for valid Rails.env methods' do expect_no_offenses(<<~RUBY) Rails.env.capitalize