Skip to content

Commit

Permalink
fixed false positive ActionControllerFlashBefore after rescue
Browse files Browse the repository at this point in the history
Co-Authored-By: Koichi ITO <koic.ito@gmail.com>
  • Loading branch information
gurix and koic committed Dec 12, 2022
1 parent 2775559 commit cb2de46
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#881](https://github.com/rubocop/rubocop-rails/pull/881): Fix a false positive for `Rails/ActionControllerFlashBeforeRender` when using `flash` in multiline `rescue` branch before `redirect_to`. ([@gurix][])
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,15 @@ def on_send(flash_node)
def followed_by_render?(flash_node)
flash_assigment_node = find_ancestor(flash_node, type: :send)
context = flash_assigment_node
if (if_node = context.each_ancestor(:if).first)
context = if_node
if (node = context.each_ancestor(:if, :rescue).first)
context = node
elsif context.right_siblings.empty?
return true
end
context = context.right_siblings

context.compact.any? do |node|
render?(node)
context.compact.any? do |render_candidate|
render?(render_candidate)
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,23 @@ def create
end
RUBY
end

it 'does not register an offense when using `flash` in multiline `rescue` branch before `redirect_to`' do
expect_no_offenses(<<~RUBY)
class HomeController < #{parent_class}
def create
begin
do_something
flash[:alert] = "msg in begin"
rescue
flash[:alert] = "msg in rescue"
end
redirect_to :index
end
end
RUBY
end
end
end

Expand Down

0 comments on commit cb2de46

Please sign in to comment.