Skip to content

Incorrect auto-correction for Rails/RedundantActiveRecordAllMethod when all has parentheses #1119

@masato-bkn

Description

@masato-bkn

There is a case where the auto-correction for Rails/RedundantActiveRecordAllMethod failed. I apologize for the recurring oversights.

When all has parentheses and Style/MethodCallWithoutArgsParentheses is disabled, running rubocop results in a failed auto-correction.

User.all().order(:created_at)

Expected behavior

The code should be corrected to:

User.order(:created_at)

Actual behavior

But instead, it gets corrected to:

User.()order(:created_at)

Steps to reproduce the problem

  • Run rubocop with only Rails/RedundantActiveRecordAllMethod enabled:
$ bundle exec rubocop --only Rails/RedundantActiveRecordAllMethod -A ./app/controllers/user_controller.rb
Calling `DidYouMean::SPELL_CHECKERS.merge!(error_name => spell_checker)' has been deprecated. Please call `DidYouMean.correct_error(error_name, spell_checker)' instead.
Inspecting 1 file
F

Offenses:

app/controllers/user_controller.rb:3:6: C: [Corrected] Rails/RedundantActiveRecordAllMethod: Redundant all detected.
User.all().order(:created_at)
     ^^^
app/controllers/user_controller.rb:3:8: F: Lint/Syntax: unexpected token tIDENTIFIER
(Using Ruby 3.2 parser; configure using TargetRubyVersion parameter, under AllCops)
User.()order(:created_at)
       ^^^^^

1 file inspected, 2 offenses detected, 1 offense corrected


result: User.()order(:created_at)
  • When Style/MethodCallWithoutArgsParentheses is enabled, this issue does not occur.
$ bundle exec rubocop --only Style/MethodCallWithoutArgsParentheses,Rails/RedundantActiveRecordAllMethod -A ./app/controllers/user_controller.rb
Calling `DidYouMean::SPELL_CHECKERS.merge!(error_name => spell_checker)' has been deprecated. Please call `DidYouMean.correct_error(error_name, spell_checker)' instead.
Inspecting 1 file
C

Offenses:

app/controllers/user_controller.rb:3:6: C: [Corrected] Rails/RedundantActiveRecordAllMethod: Redundant all detected.
User.all().order(:created_at)
     ^^^
app/controllers/user_controller.rb:3:9: C: [Corrected] Style/MethodCallWithoutArgsParentheses: Do not use parentheses for method calls with no arguments.
User.all().order(:created_at)
        ^^

1 file inspected, 2 offenses detected, 2 offenses corrected


result: User.order(:created_at)

RuboCop version

$ bundle exec rubocop -V 
Calling `DidYouMean::SPELL_CHECKERS.merge!(error_name => spell_checker)' has been deprecated. Please call `DidYouMean.correct_error(error_name, spell_checker)' instead.
1.56.0 (using Parser 3.2.2.3, rubocop-ast 1.29.0, running on ruby 3.2.2) [x86_64-darwin22]
  - rubocop-rails 2.21.1
  - rubocop-rspec 2.23.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy