False positive for warning against `public` without arguments inside method
When running the following code:
def setup # make public so we can test it Admin::UsersController.send(:public, *Admin::UsersController.protected_instance_methods) end
I'm getting the following warning:
warning: calling public without arguments inside a method may not have the intended effect
It looks like a false positive to me.
Updated by jeremyevans0 (Jeremy Evans) about 2 months ago
- Status changed from Open to Rejected
No, this actually isn't a false positive. The code is having an affect you don't want, you just don't realize it. Example:
class B end class A private B.send(:public, *) def bar end public_instance_methods(false) # [:bar] private_instance_methods(false) #  end
Yes, calling public (or other visibility method such as private/protected) without arguments changes the default visibility in the current scope, even if public was called with a receiver that doesn't match the current scope.
When calling public with arbitrary arguments, you need a guard:
unless (meths = Admin::UsersController.protected_instance_methods).empty? Admin::UsersController.send(:public, *meths) end
So the fact that it warns in your example is not a false positive, it is very much by design. If you want to change the behavior of public and other visibility methods so that such a warning is not necessary, that seems like a useful feature request, though being familiar with the code in question, I'm not sure how it would be implemented.
Updated by deivid (David Rodríguez) about 2 months ago
Oh! I looked at the code and read the warning and assumed it was a false positive since
public was being passed something, I didn't notice the array was empty! Sorry for the false alarm and thanks for the detailed explanation :)