Skip to content

False positive for ActionControllerFlashBeforeRender when flash is called in a block #1182

@HashNotAdam

Description

@HashNotAdam

When flash is called inside a block, a violation of Rails/ActionControllerFlashBeforeRender is thrown even when the action calls redirect_to.


Expected behavior

A violation should not occur when a redirect is being performed

Actual behavior

Even in a clean action (i.e. no control flow where sometimes it renders and sometimes it redirects), a violation is raised.

Steps to reproduce the problem

This action is reported as violating the rule:

def test
  messages = %w[a b c d]
  messages.each { flash[:error] = _1 }

  redirect_to(some_path)
end

The same problem occurs when called in a multi-line block:

def test
  messages = %w[a b c d]
  messages.each do |message|
    flash[:error] = message
  end

  redirect_to(some_path)
end

The issue does not occur if you assign the array directly to flash:

def test
  messages = %w[a b c d]
  flash[:error] = messages

  redirect_to(some_path)
end

RuboCop version

1.56.4 (using Parser 3.2.2.4, rubocop-ast 1.30.0, running on ruby 3.2.2) [arm64-darwin23]
  - rubocop-factory_bot 2.24.0
  - rubocop-i18n 1.14.1
  - rubocop-performance 1.19.1
  - rubocop-rails 2.22.1
  - rubocop-rspec 2.24.1

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