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

Style/AccessorGrouping safe autocorrect results in invalid code #12692

Open
a-lavis opened this issue Feb 15, 2024 · 0 comments
Open

Style/AccessorGrouping safe autocorrect results in invalid code #12692

a-lavis opened this issue Feb 15, 2024 · 0 comments
Labels

Comments

@a-lavis
Copy link

a-lavis commented Feb 15, 2024

Sometimes the Style/AccessorGrouping cop, which is marked as a safe autocorrect, corrects to incorrect code.


Expected behavior

When running rubocop -a foo.rb where foo.rb is the following file:

# frozen_string_literal: true

class Foo # :nodoc:
  attr_reader :one

  OTHER_ATTRS = %i[two three].freeze

  attr_reader(*OTHER_ATTRS)
end

I expect Style/AccessorGrouping to either not safe autocorrect, or for it to safe autocorrect to:

# frozen_string_literal: true

class Foo # :nodoc:
  OTHER_ATTRS = %i[two three].freeze

  attr_reader :one, *OTHER_ATTRS
end

Actual behavior

It safe autocorrects to:

# frozen_string_literal: true

class Foo # :nodoc:
  attr_reader :one, *OTHER_ATTRS

  OTHER_ATTRS = %i[two three].freeze
end

This code is invalid, because OTHER_ATTRS is used before it is defined.

Steps to reproduce the problem

Run rubocop -a foo.rb where foo.rb is the above file.

RuboCop version

> rubocop -V
1.60.2 (using Parser 3.3.0.5, rubocop-ast 1.30.0, running on ruby 3.3.0) [arm64-darwin23]
@koic koic added the bug label Feb 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants