Skip to content

Rails/ActionControllerFlashBeforeRender crash #1539

@cbliard

Description

@cbliard

Since bumping rubocop-rails from 2.33.3 to 2.33.4, we started seeing errors with Rails/ActionControllerFlashBeforeRender.
It's most likely related to the changes from #1528.

Minimal faulty code is

some_object
  .do_this { flash[:notice] = "hello" }
  .do_that

Steps to reproduce the problem

Run this ruby file to reproduce with the same versions:

require "bundler/inline"

gemfile(true) do
  source "https://rubygems.org"

  gem "rubocop", "= 1.81.1"
  gem "rubocop-rails", "= 2.33.4"
end

f = Tempfile.create("failing.rb")
f.write <<~RUBY
  some_object
    .do_this { flash[:notice] = "hello" }
    .do_that
RUBY
f.close

puts
puts "Running: rubocop --disable-pending-cops --debug --plugin rubocop-rails --only Rails/ActionControllerFlashBeforeRender #{f.path}"
system("rubocop --disable-pending-cops --debug --plugin rubocop-rails --only Rails/ActionControllerFlashBeforeRender #{f.path}")

File.unlink(f.path)

output:

Fetching gem metadata from https://rubygems.org/.........
Resolving dependencies...

Running: rubocop --disable-pending-cops --debug --plugin rubocop-rails --only Rails/ActionControllerFlashBeforeRender /var/folders/6n/rrd5k6gx6_g3frm64z2c9n5h0000gn/T/failing.rb20251010-68730-fupywy
For /Users/cbliard/code: Inspecting 1 file
Scanning /var/folders/6n/rrd5k6gx6_g3frm64z2c9n5h0000gn/T/failing.rb20251010-68730-fupywy
An error occurred while Rails/ActionControllerFlashBeforeRender cop was inspecting /var/folders/6n/rrd5k6gx6_g3frm64z2c9n5h0000gn/T/failing.rb20251010-68730-fupywy:2:13.
/Users/cbliard/.local/share/mise/installs/ruby/3.4.5/lib/ruby/gems/3.4.0/gems/rubocop-rails-2.33.4/lib/rubocop/cop/rails/action_controller_flash_before_render.rb:103:in 'block in RuboCop::Cop::Rails::ActionControllerFlashBeforeRender#use_redirect_to?': undefined method 'return_type?' for an instance of Symbol (NoMethodError)

            sibling = sibling.children.first if sibling.return_type? && sibling.children.one?
                                                       ^^^^^^^^^^^^^
	from /Users/cbliard/.local/share/mise/installs/ruby/3.4.5/lib/ruby/gems/3.4.0/gems/rubocop-rails-2.33.4/lib/rubocop/cop/rails/action_controller_flash_before_render.rb:101:in 'Array#any?'
	from /Users/cbliard/.local/share/mise/installs/ruby/3.4.5/lib/ruby/gems/3.4.0/gems/rubocop-rails-2.33.4/lib/rubocop/cop/rails/action_controller_flash_before_render.rb:101:in 'RuboCop::Cop::Rails::ActionControllerFlashBeforeRender#use_redirect_to?'
	from /Users/cbliard/.local/share/mise/installs/ruby/3.4.5/lib/ruby/gems/3.4.0/gems/rubocop-rails-2.33.4/lib/rubocop/cop/rails/action_controller_flash_before_render.rb:76:in 'RuboCop::Cop::Rails::ActionControllerFlashBeforeRender#followed_by_render?'
	from /Users/cbliard/.local/share/mise/installs/ruby/3.4.5/lib/ruby/gems/3.4.0/gems/rubocop-rails-2.33.4/lib/rubocop/cop/rails/action_controller_flash_before_render.rb:56:in 'RuboCop::Cop::Rails::ActionControllerFlashBeforeRender#on_send'
	from /Users/cbliard/.local/share/mise/installs/ruby/3.4.5/lib/ruby/gems/3.4.0/gems/rubocop-1.81.1/lib/rubocop/cop/commissioner.rb:143:in 'Kernel#public_send'
	from /Users/cbliard/.local/share/mise/installs/ruby/3.4.5/lib/ruby/gems/3.4.0/gems/rubocop-1.81.1/lib/rubocop/cop/commissioner.rb:143:in 'block (2 levels) in RuboCop::Cop::Commissioner#trigger_restricted_cops'
	from /Users/cbliard/.local/share/mise/installs/ruby/3.4.5/lib/ruby/gems/3.4.0/gems/rubocop-1.81.1/lib/rubocop/cop/commissioner.rb:171:in 'RuboCop::Cop::Commissioner#with_cop_error_handling'
	from /Users/cbliard/.local/share/mise/installs/ruby/3.4.5/lib/ruby/gems/3.4.0/gems/rubocop-1.81.1/lib/rubocop/cop/commissioner.rb:142:in 'block in RuboCop::Cop::Commissioner#trigger_restricted_cops'
	from /Users/cbliard/.local/share/mise/installs/ruby/3.4.5/lib/ruby/gems/3.4.0/gems/rubocop-1.81.1/lib/rubocop/cop/commissioner.rb:141:in 'Array#each'
	from /Users/cbliard/.local/share/mise/installs/ruby/3.4.5/lib/ruby/gems/3.4.0/gems/rubocop-1.81.1/lib/rubocop/cop/commissioner.rb:141:in 'RuboCop::Cop::Commissioner#trigger_restricted_cops'
	from /Users/cbliard/.local/share/mise/installs/ruby/3.4.5/lib/ruby/gems/3.4.0/gems/rubocop-1.81.1/lib/rubocop/cop/commissioner.rb:70:in 'RuboCop::Cop::Commissioner#on_send'
	from /Users/cbliard/.local/share/mise/installs/ruby/3.4.5/lib/ruby/gems/3.4.0/gems/rubocop-ast-1.47.1/lib/rubocop/ast/traversal.rb:170:in 'block in RuboCop::AST::Traversal#on_send'
	from /Users/cbliard/.local/share/mise/installs/ruby/3.4.5/lib/ruby/gems/3.4.0/gems/rubocop-ast-1.47.1/lib/rubocop/ast/traversal.rb:167:in 'Array#each'
	from /Users/cbliard/.local/share/mise/installs/ruby/3.4.5/lib/ruby/gems/3.4.0/gems/rubocop-ast-1.47.1/lib/rubocop/ast/traversal.rb:167:in 'Enumerable#each_with_index'
	from /Users/cbliard/.local/share/mise/installs/ruby/3.4.5/lib/ruby/gems/3.4.0/gems/rubocop-ast-1.47.1/lib/rubocop/ast/traversal.rb:167:in 'RuboCop::AST::Traversal#on_send'
	from /Users/cbliard/.local/share/mise/installs/ruby/3.4.5/lib/ruby/gems/3.4.0/gems/rubocop-1.81.1/lib/rubocop/cop/commissioner.rb:71:in 'RuboCop::Cop::Commissioner#on_send'
	from /Users/cbliard/.local/share/mise/installs/ruby/3.4.5/lib/ruby/gems/3.4.0/gems/rubocop-ast-1.47.1/lib/rubocop/ast/traversal.rb:168:in 'RuboCop::AST::Traversal#on_block'
	from /Users/cbliard/.local/share/mise/installs/ruby/3.4.5/lib/ruby/gems/3.4.0/gems/rubocop-1.81.1/lib/rubocop/cop/commissioner.rb:71:in 'RuboCop::Cop::Commissioner#on_block'
	from /Users/cbliard/.local/share/mise/installs/ruby/3.4.5/lib/ruby/gems/3.4.0/gems/rubocop-ast-1.47.1/lib/rubocop/ast/traversal.rb:170:in 'block in RuboCop::AST::Traversal#on_send'
	from /Users/cbliard/.local/share/mise/installs/ruby/3.4.5/lib/ruby/gems/3.4.0/gems/rubocop-ast-1.47.1/lib/rubocop/ast/traversal.rb:167:in 'Array#each'
	from /Users/cbliard/.local/share/mise/installs/ruby/3.4.5/lib/ruby/gems/3.4.0/gems/rubocop-ast-1.47.1/lib/rubocop/ast/traversal.rb:167:in 'Enumerable#each_with_index'
	from /Users/cbliard/.local/share/mise/installs/ruby/3.4.5/lib/ruby/gems/3.4.0/gems/rubocop-ast-1.47.1/lib/rubocop/ast/traversal.rb:167:in 'RuboCop::AST::Traversal#on_send'
	from /Users/cbliard/.local/share/mise/installs/ruby/3.4.5/lib/ruby/gems/3.4.0/gems/rubocop-1.81.1/lib/rubocop/cop/commissioner.rb:71:in 'RuboCop::Cop::Commissioner#on_send'
	from /Users/cbliard/.local/share/mise/installs/ruby/3.4.5/lib/ruby/gems/3.4.0/gems/rubocop-ast-1.47.1/lib/rubocop/ast/traversal.rb:20:in 'RuboCop::AST::Traversal#walk'
	from /Users/cbliard/.local/share/mise/installs/ruby/3.4.5/lib/ruby/gems/3.4.0/gems/rubocop-1.81.1/lib/rubocop/cop/commissioner.rb:87:in 'RuboCop::Cop::Commissioner#investigate'
	from /Users/cbliard/.local/share/mise/installs/ruby/3.4.5/lib/ruby/gems/3.4.0/gems/rubocop-1.81.1/lib/rubocop/cop/team.rb:174:in 'RuboCop::Cop::Team#investigate_partial'
	from /Users/cbliard/.local/share/mise/installs/ruby/3.4.5/lib/ruby/gems/3.4.0/gems/rubocop-1.81.1/lib/rubocop/cop/team.rb:108:in 'RuboCop::Cop::Team#investigate'
	from /Users/cbliard/.local/share/mise/installs/ruby/3.4.5/lib/ruby/gems/3.4.0/gems/rubocop-1.81.1/lib/rubocop/runner.rb:350:in 'block in RuboCop::Runner#inspect_file'
	from /Users/cbliard/.local/share/mise/installs/ruby/3.4.5/lib/ruby/gems/3.4.0/gems/rubocop-1.81.1/lib/rubocop/runner.rb:349:in 'Array#each'
	from /Users/cbliard/.local/share/mise/installs/ruby/3.4.5/lib/ruby/gems/3.4.0/gems/rubocop-1.81.1/lib/rubocop/runner.rb:349:in 'Enumerable#flat_map'
	from /Users/cbliard/.local/share/mise/installs/ruby/3.4.5/lib/ruby/gems/3.4.0/gems/rubocop-1.81.1/lib/rubocop/runner.rb:349:in 'RuboCop::Runner#inspect_file'
	from /Users/cbliard/.local/share/mise/installs/ruby/3.4.5/lib/ruby/gems/3.4.0/gems/rubocop-1.81.1/lib/rubocop/runner.rb:291:in 'block in RuboCop::Runner#do_inspection_loop'
	from /Users/cbliard/.local/share/mise/installs/ruby/3.4.5/lib/ruby/gems/3.4.0/gems/rubocop-1.81.1/lib/rubocop/runner.rb:326:in 'block in RuboCop::Runner#iterate_until_no_changes'
	from <internal:kernel>:168:in 'Kernel#loop'
	from /Users/cbliard/.local/share/mise/installs/ruby/3.4.5/lib/ruby/gems/3.4.0/gems/rubocop-1.81.1/lib/rubocop/runner.rb:319:in 'RuboCop::Runner#iterate_until_no_changes'
	from /Users/cbliard/.local/share/mise/installs/ruby/3.4.5/lib/ruby/gems/3.4.0/gems/rubocop-1.81.1/lib/rubocop/runner.rb:287:in 'RuboCop::Runner#do_inspection_loop'
	from /Users/cbliard/.local/share/mise/installs/ruby/3.4.5/lib/ruby/gems/3.4.0/gems/rubocop-1.81.1/lib/rubocop/runner.rb:167:in 'block in RuboCop::Runner#file_offenses'
	from /Users/cbliard/.local/share/mise/installs/ruby/3.4.5/lib/ruby/gems/3.4.0/gems/rubocop-1.81.1/lib/rubocop/runner.rb:192:in 'RuboCop::Runner#file_offense_cache'
	from /Users/cbliard/.local/share/mise/installs/ruby/3.4.5/lib/ruby/gems/3.4.0/gems/rubocop-1.81.1/lib/rubocop/runner.rb:166:in 'RuboCop::Runner#file_offenses'
	from /Users/cbliard/.local/share/mise/installs/ruby/3.4.5/lib/ruby/gems/3.4.0/gems/rubocop-1.81.1/lib/rubocop/runner.rb:154:in 'RuboCop::Runner#process_file'
	from /Users/cbliard/.local/share/mise/installs/ruby/3.4.5/lib/ruby/gems/3.4.0/gems/rubocop-1.81.1/lib/rubocop/runner.rb:135:in 'block in RuboCop::Runner#each_inspected_file'
	from /Users/cbliard/.local/share/mise/installs/ruby/3.4.5/lib/ruby/gems/3.4.0/gems/rubocop-1.81.1/lib/rubocop/runner.rb:134:in 'Array#each'
	from /Users/cbliard/.local/share/mise/installs/ruby/3.4.5/lib/ruby/gems/3.4.0/gems/rubocop-1.81.1/lib/rubocop/runner.rb:134:in 'Enumerable#reduce'
	from /Users/cbliard/.local/share/mise/installs/ruby/3.4.5/lib/ruby/gems/3.4.0/gems/rubocop-1.81.1/lib/rubocop/runner.rb:134:in 'RuboCop::Runner#each_inspected_file'
	from /Users/cbliard/.local/share/mise/installs/ruby/3.4.5/lib/ruby/gems/3.4.0/gems/rubocop-1.81.1/lib/rubocop/runner.rb:120:in 'RuboCop::Runner#inspect_files'
	from /Users/cbliard/.local/share/mise/installs/ruby/3.4.5/lib/ruby/gems/3.4.0/gems/rubocop-1.81.1/lib/rubocop/runner.rb:73:in 'RuboCop::Runner#run'
	from /Users/cbliard/.local/share/mise/installs/ruby/3.4.5/lib/ruby/gems/3.4.0/gems/rubocop-1.81.1/lib/rubocop/cli/command/execute_runner.rb:26:in 'block in RuboCop::CLI::Command::ExecuteRunner#execute_runner'
	from /Users/cbliard/.local/share/mise/installs/ruby/3.4.5/lib/ruby/gems/3.4.0/gems/rubocop-1.81.1/lib/rubocop/cli/command/execute_runner.rb:52:in 'RuboCop::CLI::Command::ExecuteRunner#with_redirect'
	from /Users/cbliard/.local/share/mise/installs/ruby/3.4.5/lib/ruby/gems/3.4.0/gems/rubocop-1.81.1/lib/rubocop/cli/command/execute_runner.rb:25:in 'RuboCop::CLI::Command::ExecuteRunner#execute_runner'
	from /Users/cbliard/.local/share/mise/installs/ruby/3.4.5/lib/ruby/gems/3.4.0/gems/rubocop-1.81.1/lib/rubocop/cli/command/execute_runner.rb:17:in 'RuboCop::CLI::Command::ExecuteRunner#run'
	from /Users/cbliard/.local/share/mise/installs/ruby/3.4.5/lib/ruby/gems/3.4.0/gems/rubocop-1.81.1/lib/rubocop/cli/command.rb:11:in 'RuboCop::CLI::Command.run'
	from /Users/cbliard/.local/share/mise/installs/ruby/3.4.5/lib/ruby/gems/3.4.0/gems/rubocop-1.81.1/lib/rubocop/cli/environment.rb:18:in 'RuboCop::CLI::Environment#run'
	from /Users/cbliard/.local/share/mise/installs/ruby/3.4.5/lib/ruby/gems/3.4.0/gems/rubocop-1.81.1/lib/rubocop/cli.rb:128:in 'RuboCop::CLI#run_command'
	from /Users/cbliard/.local/share/mise/installs/ruby/3.4.5/lib/ruby/gems/3.4.0/gems/rubocop-1.81.1/lib/rubocop/cli.rb:135:in 'RuboCop::CLI#execute_runners'
	from /Users/cbliard/.local/share/mise/installs/ruby/3.4.5/lib/ruby/gems/3.4.0/gems/rubocop-1.81.1/lib/rubocop/cli.rb:54:in 'block in RuboCop::CLI#run'
	from /Users/cbliard/.local/share/mise/installs/ruby/3.4.5/lib/ruby/gems/3.4.0/gems/rubocop-1.81.1/lib/rubocop/cli.rb:87:in 'RuboCop::CLI#profile_if_needed'
	from /Users/cbliard/.local/share/mise/installs/ruby/3.4.5/lib/ruby/gems/3.4.0/gems/rubocop-1.81.1/lib/rubocop/cli.rb:45:in 'RuboCop::CLI#run'
	from /Users/cbliard/.local/share/mise/installs/ruby/3.4.5/lib/ruby/gems/3.4.0/gems/rubocop-1.81.1/exe/rubocop:15:in '<top (required)>'
	from /Users/cbliard/.local/share/mise/installs/ruby/3.4.5/bin/rubocop:25:in 'Kernel#load'
	from /Users/cbliard/.local/share/mise/installs/ruby/3.4.5/bin/rubocop:25:in '<main>'
.

1 file inspected, no offenses detected

1 error occurred:
An error occurred while Rails/ActionControllerFlashBeforeRender cop was inspecting /var/folders/6n/rrd5k6gx6_g3frm64z2c9n5h0000gn/T/failing.rb20251010-68730-fupywy:2:13.
configuration from /Users/cbliard/.local/share/mise/installs/ruby/3.4.5/lib/ruby/gems/3.4.0/gems/rubocop-1.81.1/config/default.yml
Errors are usually caused by RuboCop bugs.
Please, update to the latest RuboCop version if not already in use, and report a bug if the issue still occurs on this version.
https://github.com/rubocop/rubocop/issues

Mention the following information in the issue report:
1.81.1 (using Parser 3.3.9.0, Prism 1.5.2, default, rubocop-ast 1.47.1, analyzing as Ruby 3.3, running on ruby 3.4.5) [arm64-darwin24]
Finished in 0.21660399995744228 seconds

Expected behavior

It should not produce an error.
Not sure exactly what the output should be with the code I provided.

Actual behavior

Fails with

/Users/cbliard/.local/share/mise/installs/ruby/3.4.5/lib/ruby/gems/3.4.0/gems/rubocop-rails-2.33.4/lib/rubocop/cop/rails/action_controller_flash_before_render.rb:103:in 'block in RuboCop::Cop::Rails::ActionControllerFlashBeforeRender#use_redirect_to?': undefined method 'return_type?' for an instance of Symbol (NoMethodError)

            sibling = sibling.children.first if sibling.return_type? && sibling.children.one?
                                                       ^^^^^^^^^^^^^

The sibling value is :do_that.
The context.right_siblings value is [:do_that] (from 2 lines above).

RuboCop version

1.81.1 (using Parser 3.3.9.0, Prism 1.5.2, default, rubocop-ast 1.47.1, analyzing as Ruby 3.3, running on ruby 3.4.5) [arm64-darwin24]

rubocop-rails version is 2.33.4

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