-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Endless Method Keyword/Operator Precedence Check #12988
Comments
Such a cop is badly needed. Our team has run into def foo = 42 if cond
# NoMethodError: undefined method `cond' for main |
@vlad-pisanov I'm looking into this one. There is a somewhat common pattern of defining methods unless defined, such as: def foo
42
end unless defined?(:foo) which could be written in endless form potentially: def foo = 42 unless defined?(:foo) How would you handle that? Exception in the cop specifically for |
@dvandersluis I haven't encountered this pattern before. Maybe it could be conditionally permitted for endless methods with a cop option like |
Perhaps I'm misremembering its prevalance. A github code search reveals some uses but not as prevalent as I thought. There's also I think I'll just leave it for now and if issues come up we can address. |
FWIW, def foo
42
end unless defined?(:foo) seem like major anti-patterns. |
Agreed it's an anti-pattern, just wanted to avoid unexpected behaviour. |
Is your feature request related to a problem?
Consider the following code
Because the
=
operator has higher precedence thanand
oror
, they will not be included as part the method.Describe the solution you'd like
A rubocop check to detect operators/keywords with lower precedence than
=
on the same line as an endless expression would be incredibly helpful in avoiding this sort of footgun.The text was updated successfully, but these errors were encountered: