rubocop icon indicating copy to clipboard operation
rubocop copied to clipboard

Lint/Void autocorrection is unsafe when a constant is referenced

Open jonleighton opened this issue 2 years ago • 1 comments

When I run rubocop -a on this file:

Foo::Bar

class Foo::Bar
end

It rewrites the file like so:

class Foo::Bar
end

However, this is unsafe, due to Ruby's constant autoloading mechanism.

For example, in a Rails app, the constant may be referenced to force it to be loaded from another file, before apply a monkey-patch.


Expected behavior

Either the file is unchanged, or the change is only applied when unsafe autocorrections are used.

Actual behavior

See above.

Steps to reproduce the problem

  1. Put the above into test.rb
  2. Run rubocop -a test.rb

RuboCop version

1.54.0 (using Parser 3.2.2.3, rubocop-ast 1.29.0, running on ruby 3.2.2) [aarch64-linux-musl]

jonleighton avatar Jul 03 '23 05:07 jonleighton

This issue has been automatically marked as stale because it has not had any recent activity. It will be closed soon if no further activity occurs. Thank you for your understanding!

github-actions[bot] avatar Jun 15 '25 02:06 github-actions[bot]

This issue has been automatically closed due to lack of activity. Feel free to ask for it to be re-opened if you ever come back to it.

github-actions[bot] avatar Jul 15 '25 02:07 github-actions[bot]