Style/UnneededCondition cop should exclude elsif.
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.
Rubocop reports a violation for the first line of the snippet.
Please use the snippet above.
$ rubocop -V
0.57.0 (using Parser 2.5.1.0, running on ruby 2.4.3 x86_64-darwin16)
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. 🎩
Most helpful comment
Autocorrect also doesn’t work in this case:
Fixed using
rubocop --only Style/UnneededCondition -a test.rbbecomes…which is no longer valid Ruby.