rubocop: AndOr sees `render and return` as a violation
How can I configure RuboCop to no longer see the code below as a violation to the AndOr check?
render nothing: true, status: :bad_request and return
Currently it tells me to replace and with &&, which is not correct as far as I know.
About this issue
- Original URL
- State: closed
- Created 10 years ago
- Reactions: 8
- Comments: 17 (4 by maintainers)
This conflicts with the current Rails guides, meaning this issue should probably be reopened.
This is not the case. I agree, if your intention is to always return then yes, use
;. Otherwise you have some kind of expectation from the left-hand side and you can useand/orto do something:That’s a trivial example which can easily be replaced with
if/unlessbut I hope it illustrates that;is notand.It’s true that the intention for
renderis to always return. But by using;you’re not chaining the operations together. This is evident if you have a conditional:I don’t think it’s fair to call
and returna hack or a workaround. Sureand/orcan cause developers grief when used in a conditional, but I think they have a place when used as flow control operators (as in the case ofand return). I’ve always had the impression thatand returnwas considered idiomatic Ruby.Or
Part of the problem here is that the Rails community have decided that the
andoperator has different semantics than that which theandoperator was originally intended for. Whereas&&is still viewed as the logical and,andhas come to mean ‘do something and then do something else’.@uri It depends. If your intention is to always return and you’re using
andas a more visually pleasing alternative to;, depending on the left hand side to always return a truthy value, then I think it’s an ugly hack. @mikegee said this earlier in the discussion.Where would the parens go in this case?
I prefer to put the
returnon the next line. I’m not sure whyand returnbecame ok afterrender&redirect_toin the Rails community. It looks like a hack to me.Also, I think
render nothing: true, status: :bad_requestis the same ashead :bad_request.