Project

General

Profile

Bug #16969

False positive for warning against `public` without arguments inside method

Added by deivid (David Rodríguez) about 2 months ago. Updated about 2 months ago.

Status:
Rejected
Priority:
Normal
Assignee:
-
Target version:
-
ruby -v:
ruby 2.7.1p83 (2020-03-31 revision a0c7c23c9c) [x86_64-linux]
[ruby-core:98868]

Description

Hi!

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.

I'm guessing it's a bug in https://bugs.ruby-lang.org/projects/ruby-master/repository/git/revisions/2993b24a1ecc5fa3cc9f140bfd80669c3a3b7b9c.

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 :)

Also available in: Atom PDF