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 e730348
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 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,8 +69,8 @@ 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 (ancestor_node = context.each_ancestor(:if, :rescue).first)
context = ancestor_node
elsif context.right_siblings.empty?
return true
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 e730348

Please sign in to comment.