rubocop / rubocop-rails

A RuboCop extension focused on enforcing Rails best practices and coding conventions.
https://docs.rubocop.org/rubocop-rails
MIT License
819 stars 263 forks source link

False Positive for Rails/ActionControllerFlashBeforeRender when flash is called in different method #1360

Closed ildarkayumov closed 1 month ago

ildarkayumov commented 2 months ago

Expected behavior

When redirect_to is called in controller action and flash is set in different extracted method, the cop should not show any offense.

Actual behavior

Even if methods are very simple implementation, a violation is raised.

Inspecting 1 file
C

Offenses:

app/controllers/users_controller.rb:54:5: C: [Correctable] Rails/ActionControllerFlashBeforeRender: Use flash.now before render.
    flash[:notice] = t(".notice")

Steps to reproduce the problem

  def update
    user.update(user_params)

    prepare_flash_message

    redirect_to users_path
  end

  def prepare_flash_message
    flash[:notice] = t(".notice")
  end

RuboCop version

$ rubocop -V
1.65.1 (using Parser 3.3.5.0, rubocop-ast 1.32.3, running on ruby 3.3.3) [arm64-darwin23]
  - rubocop-capybara 2.21.0
  - rubocop-factory_bot 2.26.1
  - rubocop-performance 1.21.1
  - rubocop-rails 2.26.1
  - rubocop-rspec 3.0.5
  - rubocop-rspec_rails 2.30.0
mjankowski commented 1 month ago

May be fixed by this revert - https://github.com/rubocop/rubocop-rails/pull/1344 - but is not released yet.

koic commented 1 month ago

I plan to release a bug fix version within the next couple of days. Please wait a little longer

Earlopain commented 1 month ago

The PR will not fix this. There is simply no way for rubocop to handle this properly; it is just static analysis. You are probably better of disabling it if this is a common pattern for you.