From ad0fec1f0d8afc08163e92ce9fabf23fa263169a Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Wed, 22 Feb 2023 13:28:17 +0900 Subject: [PATCH] [Fix #909] Fix a false positive for `Rails/ActionControllerFlashBeforeRender` Fixes #909. This PR fixes a false positive for `Rails/ActionControllerFlashBeforeRender` when using `flash` before `redirect_to` in `if` branch. --- ...ils_action_controller_flash_before_render.md | 1 + .../action_controller_flash_before_render.rb | 8 ++++++++ ...ction_controller_flash_before_render_spec.rb | 17 +++++++++++++++++ 3 files changed, 26 insertions(+) create mode 100644 changelog/fix_a_false_positive_for_rails_action_controller_flash_before_render.md diff --git a/changelog/fix_a_false_positive_for_rails_action_controller_flash_before_render.md b/changelog/fix_a_false_positive_for_rails_action_controller_flash_before_render.md new file mode 100644 index 0000000000..924ebc93d4 --- /dev/null +++ b/changelog/fix_a_false_positive_for_rails_action_controller_flash_before_render.md @@ -0,0 +1 @@ +* [#909](https://github.com/rubocop/rubocop-rails/issues/909): Fix a false positive for `Rails/ActionControllerFlashBeforeRender` when using `flash` before `redirect_to` in `if` branch. ([@koic][]) diff --git a/lib/rubocop/cop/rails/action_controller_flash_before_render.rb b/lib/rubocop/cop/rails/action_controller_flash_before_render.rb index 74b01333d8..589d33795d 100644 --- a/lib/rubocop/cop/rails/action_controller_flash_before_render.rb +++ b/lib/rubocop/cop/rails/action_controller_flash_before_render.rb @@ -70,6 +70,8 @@ def followed_by_render?(flash_node) flash_assignment_node = find_ancestor(flash_node, type: :send) context = flash_assignment_node if (node = context.each_ancestor(:if, :rescue).first) + return false if use_redirect_to?(context) + context = node elsif context.right_siblings.empty? return true @@ -95,6 +97,12 @@ def instance_method_or_block?(node) def_node || block_node end + def use_redirect_to?(context) + context.right_siblings.compact.any? do |sibling| + sibling.send_type? && sibling.method?(:redirect_to) + end + end + def find_ancestor(node, type:) node.each_ancestor(type).first end diff --git a/spec/rubocop/cop/rails/action_controller_flash_before_render_spec.rb b/spec/rubocop/cop/rails/action_controller_flash_before_render_spec.rb index 9b91562bcc..4e1dd01a20 100644 --- a/spec/rubocop/cop/rails/action_controller_flash_before_render_spec.rb +++ b/spec/rubocop/cop/rails/action_controller_flash_before_render_spec.rb @@ -202,6 +202,23 @@ def create RUBY end + it 'does not register an offense when using `flash` before `redirect_to` in `if` branch' do + expect_no_offenses(<<~RUBY) + class HomeController < #{parent_class} + def create + if condition + flash[:alert] = "msg" + redirect_to :index + + return + end + + render :index + end + 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}