Skip to content
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

false positive for Lint/ToEnumArguments when using sorbet #11591

Closed
dduugg opened this issue Feb 18, 2023 · 0 comments · Fixed by #11592
Closed

false positive for Lint/ToEnumArguments when using sorbet #11591

dduugg opened this issue Feb 18, 2023 · 0 comments · Fixed by #11592

Comments

@dduugg
Copy link
Contributor

dduugg commented Feb 18, 2023

Expected behavior

The "good" example for Lint/ToEnumArguments looks like this:

def foo(x, y = 1)
  to_enum(__callee__, x, y)
end

However, since __callee__ and __method__ can return nil, these are not viable in a type-checked ruby context, Sorbet in particular:

https://sorbet.run/#%23%20frozen_string_literal%3A%20true%0A%0Aextend%20T%3A%3ASig%0A%0Adef%20foo%28x%2C%20y%20%3D%201%29%0A%20%20to_enum%28__callee__%2C%20x%2C%20y%29%0Aend%0A%0Adef%20bar%28x%2C%20y%20%3D%201%29%0A%20%20to_enum%28T.must%28__callee__%29%2C%20x%2C%20y%29%0Aend

Instead, the __callee__ invocation has to be asserted as non-nil, by wrapping in T.must:

def for(x, y = 1)
  to_enum(T.must(__callee__), x, y)
end

I believe that this formulation should be accepted by rubocop.

Actual behavior

The last example above triggers a rubocop violation:

foo.rb:10:3: W: Lint/ToEnumArguments: Ensure you correctly provided all the arguments.
  to_enum(T.must(__callee__), x, y)
  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Steps to reproduce the problem

Invoke rubocop foo.rb --only Lint/ToEnumArguments, where foo.rb contains the following:

def bar(x, y = 1)
  to_enum(T.must(__callee__), x, y)
end

RuboCop version

$ rubocop -V
1.45.1 (using Parser 3.2.0.0, rubocop-ast 1.24.1, running on ruby 3.1.1) [x86_64-darwin21]
koic added a commit to koic/rubocop that referenced this issue Feb 18, 2023
Fixes rubocop#11591.

THis PR fixes a false positive for `Lint/ToEnumArguments`
when enumerator is not created for `__callee__` and `__callee__` methods.
bbatsov pushed a commit that referenced this issue Feb 22, 2023
Fixes #11591.

THis PR fixes a false positive for `Lint/ToEnumArguments`
when enumerator is not created for `__callee__` and `__callee__` methods.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant