rubocop-rails icon indicating copy to clipboard operation
rubocop-rails copied to clipboard

Confusing message for `Rails/RootPathnameMethods` in `Dir[]`

Open Tietew opened this issue 6 months ago • 4 comments

RuboCop suggests confusing solution on following code:

files = Dir[Rails.root.join('path/*')]

Expected behavior

RuboCop suggests to rewrite Dir[Rails.root.join(...)] to Rails.root.glob(...) as autocorrection does.

Actual behavior

RuboCop suggests to append #[].

files = Dir[Rails.root.join('path/*')]
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
C: [Correctable] Rails/RootPathnameMethods: Rails.root is a Pathname so you can just append #[].

But appending #[] is an incorrect solution.

Note

Autocorrection works correctly. The issue is only in the message.

# bad
files = Dir[Rails.root.join('path/*')]
# autocorrect
files = Rails.root.glob('path/*')

RuboCop version

$ be rubocop -V
1.65.1 (using Parser 3.3.4.2, rubocop-ast 1.32.1, running on ruby 3.3.4) [x86_64-linux]
  - rubocop-capybara 2.21.0
  - rubocop-factory_bot 2.26.1
  - rubocop-performance 1.21.1
  - rubocop-rails 2.26.0
  - rubocop-rake 0.6.0
  - rubocop-rspec 3.0.4
  - rubocop-rspec_rails 2.30.0

Tietew avatar Aug 27 '24 10:08 Tietew