rubocop/rails-style-guide

New cop: Every AR find must either be in a conditional or be a bang

andyw8 opened this issue · 1 comments

This was an idea from @avdi:

https://twitter.com/avdi/status/1202702640397856768?ref_src=twsrc%5Etfw

Idea for a Rails linting rule: every AR find must either be in a conditional or be a bang

Either:

widget = Widget.find_by! color: "green"

Or

if(widget = Widget.find_by color: "green")
# ...

There's no reason to ever leave the discovery of a nil for later. Is there?

There's some interesting discussion in the replies.

It's similar to the existing SaveBang rule.

koic commented

I wonder if this can be generalized to Rails style guide coding rule before creating a new cop. I will transfer this issue to the style guide repo.