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

False positive for ActionControllerFlashBeforeRender when render and redirect_to are both present #808

Closed
chris-hewitt opened this issue Oct 12, 2022 · 0 comments · Fixed by #812
Labels
bug Something isn't working

Comments

@chris-hewitt
Copy link

ActionControllerFlashBeforeRender also triggers when flash[] comes after render. This causes a false positive for a scenario such as:

    render :index and return if foo
    flash[:alert] = "msg"
    redirect_to "https://www.example.com/"

(It looks the cop's followed_by_render? method currently behaves more like a sibling_of_render?)


Expected behavior

0 failures

Actual behavior

Offenses:

[Correctable] Rails/ActionControllerFlashBeforeRender: Use flash.now before render.
    flash[:alert] = "msg"
    ^^^^^

Steps to reproduce the problem

RSpec.describe RuboCop::Cop::Rails::ActionControllerFlashBeforeRender, :config do
  ...
  context 'when using `flash` before `redirect_to`' do
    ...
    context 'when `render` is also present earlier in the method' do
      it 'does not register an offense' do # <-- FAILS
        expect_no_offenses(<<~RUBY)
          class HomeController < ApplicationController
            def create
              render :index and return unless foo
              flash[:alert] = "msg"
              redirect_to "https://www.example.com/"
            end
          end
        RUBY
      end
    end
end

RuboCop version

$ bundle exec rubocop -V
1.36.0 (using Parser 3.1.2.1, rubocop-ast 1.21.0, running on ruby 2.7.6) [x86_64-linux]
  - rubocop-performance 1.15.0
  - rubocop-rails 2.16.1
  - rubocop-rspec 2.13.2
@koic koic added the bug Something isn't working label Oct 12, 2022
americodls added a commit to americodls/rubocop-rails that referenced this issue Oct 12, 2022
americodls added a commit to americodls/rubocop-rails that referenced this issue Oct 12, 2022
americodls added a commit to americodls/rubocop-rails that referenced this issue Oct 12, 2022
americodls added a commit to americodls/rubocop-rails that referenced this issue Oct 13, 2022
@koic koic closed this as completed in #812 Oct 14, 2022
koic added a commit that referenced this issue Oct 14, 2022
[Fix #808] Flash is accepted only if followed by redirect_to
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants