Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Follow up fix for positive for Rails/ActionControllerFlashBeforeRender in rescue block #881

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
gurix marked this conversation as resolved.
Show resolved Hide resolved
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