False warning for Style/AndOr cop when used as chaining operation
Example:
# Case 1 def show redirect_to 'error' and return if params.has?(:api_key) end # Redirected to /error # Case 2 def show redirect_to 'error' && return if params.has?(:api_key) end # Rendering show.html.erb
Case #1 is our expected result and case #2 is not. In case #2, "&& return" will not work because && has much higher precedence level.
Reference: https://ruby-doc.org/core-2.4.1/doc/syntax/precedence_rdoc.html
So when rubocop suggests to change "and" to "&&" you need to simulate the result if it is what you want before replacing. At this point in time, rubocop has not provided a way to skip this warning for "and return" scenario. A simple way to hide the warning is to split the return call into another line. E.g.
def show unless params.has?(:api_key) redirect_to 'error' return end end
AI Summary
Chrome On-device AI
2024-10-04 22:17:12
Share Article