Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rails/EnumSyntax autocorrection causes deprecation warning on Ruby 2.7 #1362

Closed
r7kamura opened this issue Sep 12, 2024 · 0 comments · Fixed by #1363
Closed

Rails/EnumSyntax autocorrection causes deprecation warning on Ruby 2.7 #1362

r7kamura opened this issue Sep 12, 2024 · 0 comments · Fixed by #1363
Labels
bug Something isn't working

Comments

@r7kamura
Copy link
Contributor

r7kamura commented Sep 12, 2024

Rails/EnumSyntax does the following autocorrection:

it 'autocorrects' do
expect_offense(<<~RUBY)
enum status: { active: 0, archived: 1 }
^^^^^^^^^^^^^^^^^^^^^^^^^^ Enum defined with keyword arguments in `status` enum declaration. Use positional arguments instead.
RUBY
expect_correction(<<~RUBY)
enum :status, { active: 0, archived: 1 }
RUBY
end

However, if you run the autocorrected code in Ruby 2.7, you will get a deprecated warning about the usage of keyword arguments, depending on the warning options (e.g. -W:deprecated) and ruby versions.

Expected behavior

No warnings should be reported after autocorrection.

In other words, code that originally did not have a deprecation warning should not be changed by autocorrect to a state where a deprecation warning is issued.

Actual behavior

The following warning is reported after its autocorrection:

warning: Using the last argument as keyword parameters is deprecated; maybe ** should be added to the call
/home/r7kamura/.rbenv/versions/2.7.8/lib/ruby/gems/2.7.0/gems/activerecord-7.1.4/lib/active_record/enum.rb:216: warning: The called method `enum' is defined here

Steps to reproduce the problem

You can verify that this warning is output by running the following code in Ruby 2.7:

# frozen_string_literal: true

require "bundler/inline"

gemfile do
  source "https://rubygems.org"

  gem "rails"
  gem "sqlite3", "< 1.7" # sqlite3 1.7+ doesn't support Ruby 2.7.
end

require "active_record"

ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")

ActiveRecord::Schema.define do
  create_table :posts, force: true do |t|
    t.integer :status
  end
end

Warning[:deprecated] = true

class Post < ActiveRecord::Base
  enum :status, { active: 0, archived: 1 }
end

RuboCop version

$ bundle exec rubocop -V
1.65.1 (using Parser 3.3.0.5, rubocop-ast 1.31.2, running on ruby 2.7.8) [x86_64-linux]
  - rubocop-rails 2.26.0
@koic koic added the bug Something isn't working label Sep 12, 2024
koic added a commit to koic/rubocop-rails that referenced this issue Sep 12, 2024
Fixes rubocop#1362.

This PR fixes false positives for `Rails/EnumSyntax` when using Ruby 2.7.

The warning shown in rubocop#1238 appears starting from Rails 7.2 (Requires Ruby 3.1+).
On the other hand, if users use Ruby 2.7, the warning reported in rubocop#1362 will be displayed.

Therefore, it would be appropriate to enable this cop only when analyzing Ruby 3.0+.

Nothing will happen when using Ruby 2.7 with Rails 7.0 or Rails 7.1, but the warning in rubocop#1238
will not be displayed either. Meanwhile, in Rails 7.2, which requires Ruby 3.1+, Ruby 2.7 cannot be used, so there is no issue.
@koic koic closed this as completed in 50fe9a7 Sep 12, 2024
koic added a commit that referenced this issue Sep 12, 2024
…_syntax

[Fix #1362] Fix false positives for `Rails/EnumSyntax`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants