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

[Fix #1244] Fix a false positive for Rails/ActionControllerFlashBeforeRender when returning redirect_to #1246

Merged
merged 1 commit into from
Mar 4, 2024
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 @@
* [#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