Skip to content

Commit

Permalink
Merge pull request #1246 from Earlopain/fix-1244
Browse files Browse the repository at this point in the history
[Fix #1244] Fix a false positive for `Rails/ActionControllerFlashBeforeRender` when returning `redirect_to`
  • Loading branch information
koic authored Mar 4, 2024
2 parents 87d94e9 + af82326 commit 56d11bd
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#1244](https://github.com/rubocop/rubocop-rails/issues/1244): Fix a false positive for `Rails/ActionControllerFlashBeforeRender` when returning `redirect_to`. ([@earlopain][])
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ def instance_method_or_block?(node)

def use_redirect_to?(context)
context.right_siblings.compact.any? do |sibling|
# Unwrap `return redirect_to :index`
sibling = sibling.children.first if sibling.return_type? && sibling.children.one?
sibling.send_type? && sibling.method?(:redirect_to)
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -312,4 +312,20 @@ def create
RUBY
end
end

context 'when using `flash` after `render` and returning `redirect_to` in condition block' do
it 'does not register an offense' do
expect_no_offenses(<<~RUBY)
class HomeController < ApplicationController
def create
if condition
flash[:alert] = "msg"
return redirect_to "https://www.example.com/"
end
render :index
end
end
RUBY
end
end
end

0 comments on commit 56d11bd

Please sign in to comment.