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

Style/GuardClause fails with elsif and raise #11196

Closed
JasonBarnabe opened this issue Nov 21, 2022 · 0 comments · Fixed by #11197
Closed

Style/GuardClause fails with elsif and raise #11196

JasonBarnabe opened this issue Nov 21, 2022 · 0 comments · Fixed by #11197
Labels

Comments

@JasonBarnabe
Copy link

Style/GuardClause auto-correction results in syntax errors with the following code:

def call(method, *args)
  response = self.class.send(method, *args)

  if response.code == 401
    raise AuthorizationFailure
  elsif response.code == 404
    raise NotFound
  elsif response['errors'].present?
    raise UnexpectedResponse, "unexpected response: #{response.inspect}"
  else
    response
  end
end

Expected behavior

Correction to:

def call(method, *args)
  response = self.class.send(method, *args)

  raise AuthorizationFailure if response.code == 401

  raise NotFound if response.code == 404
    
  raise UnexpectedResponse, "unexpected response: #{response.inspect}" if response['errors'].present?

  response
end

Actual behavior

$ rubocop -A --only Style/GuardClause --debug ./rubocopbug.rb 
For /www/Simplero: configuration from /www/Simplero/.rubocop.yml
configuration from /home/jason/.rbenv/versions/2.7.6/lib/ruby/gems/2.7.0/gems/rubocop-minitest-0.23.2/config/default.yml
configuration from /home/jason/.rbenv/versions/2.7.6/lib/ruby/gems/2.7.0/gems/rubocop-minitest-0.23.2/config/default.yml
Default configuration from /home/jason/.rbenv/versions/2.7.6/lib/ruby/gems/2.7.0/gems/rubocop-1.39.0/config/default.yml
configuration from /home/jason/.rbenv/versions/2.7.6/lib/ruby/gems/2.7.0/gems/rubocop-performance-1.15.1/config/default.yml
configuration from /home/jason/.rbenv/versions/2.7.6/lib/ruby/gems/2.7.0/gems/rubocop-performance-1.15.1/config/default.yml
configuration from /home/jason/.rbenv/versions/2.7.6/lib/ruby/gems/2.7.0/gems/rubocop-rails-2.17.3/config/default.yml
configuration from /home/jason/.rbenv/versions/2.7.6/lib/ruby/gems/2.7.0/gems/rubocop-rails-2.17.3/config/default.yml
Use parallel by default.
Skipping parallel inspection: only a single file needs inspection
Inspecting 1 file
Scanning /www/Simplero/rubocopbug.rb
Loading cache from /home/jason/.cache/rubocop_cache/4c1cd43e90e17aa86a0cd840b2f8c640d820033f/4e5ae3791c1d8e384badf81b88153dccf260d162/7a1361acb3ce8e58b0745c992ceaa05f89355179
C

Offenses:

rubocopbug.rb:4:3: C: [Corrected] Style/GuardClause: Use a guard clause (raise AuthorizationFailure if response.code == 401) instead of wrapping the code inside a conditional expression.
  if response.code == 401
  ^^

1 file inspected, 1 offense detected, 1 offense corrected
Finished in 0.4634500271640718 seconds
def call(method, *args)
  response = self.class.send(method, *args)

  raise AuthorizationFailure if response.code == 401
    
   response.code == 404
    raise NotFound
  elsif response['errors'].present?
    raise UnexpectedResponse, "unexpected response: #{response.inspect}"
  else
    response
  
end

RuboCop version

$ rubocop -V
1.39.0 (using Parser 3.1.2.1, rubocop-ast 1.23.0, running on ruby 2.7.6) [x86_64-linux]
  - rubocop-minitest 0.23.2
  - rubocop-performance 1.15.1
  - rubocop-rails 2.17.3
@koic koic added the bug label Nov 21, 2022
koic added a commit to koic/rubocop that referenced this issue Nov 21, 2022
Fixes rubocop#11196.

This PR fix a false positive for `Style/GuardClause`
when using `raise` in `then` body of `if..elsif..end` form.

It would be unexpected to replace complex conditional forms
using `elsif` with modifier form.
bbatsov pushed a commit that referenced this issue Nov 22, 2022
Fixes #11196.

This PR fix a false positive for `Style/GuardClause`
when using `raise` in `then` body of `if..elsif..end` form.

It would be unexpected to replace complex conditional forms
using `elsif` with modifier form.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants