Rubocop: Style/UnneededCondition should exclude elsif

Created on 6 Jun 2018  Â·  4Comments  Â·  Source: rubocop-hq/rubocop

Style/UnneededCondition cop should exclude elsif.


Expected behavior

url = if params[:foo]
  params[:foo]
elsif some_other_condition?
  do_this
  interpolate_url
end

Style/UnneededCondition should ignore this condition because it's hard to write a one liner of the code above.

Actual behavior

Rubocop reports a violation for the first line of the snippet.

Steps to reproduce the problem

Please use the snippet above.

RuboCop version

$ rubocop -V
0.57.0 (using Parser 2.5.1.0, running on ruby 2.4.3 x86_64-darwin16)

Most helpful comment

Autocorrect also doesn’t work in this case:

url = if params[:foo]
  params[:foo]
elsif some_other_condition?
  do_this
  interpolate_url
end

Fixed using rubocop --only Style/UnneededCondition -a test.rb becomes…

url = params[:foo] || elsif some_other_condition?
  do_this
  interpolate_url

which is no longer valid Ruby.

All 4 comments

Thanks for the feedback. I think so too.
I confirmed this reproduction. I will open a PR. Thanks!

Autocorrect also doesn’t work in this case:

url = if params[:foo]
  params[:foo]
elsif some_other_condition?
  do_this
  interpolate_url
end

Fixed using rubocop --only Style/UnneededCondition -a test.rb becomes…

url = params[:foo] || elsif some_other_condition?
  do_this
  interpolate_url

which is no longer valid Ruby.

This case was changed to be ignored by PR #5957. It will not be autocorrected by that.

Thank you all for the quick fix. 🎩

Was this page helpful?
0 / 5 - 0 ratings

Related issues

bbugh picture bbugh  Â·  3Comments

david942j picture david942j  Â·  3Comments

millisami picture millisami  Â·  3Comments

bquorning picture bquorning  Â·  3Comments

NobodysNightmare picture NobodysNightmare  Â·  3Comments