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

Incorrect example code for Lint/UselessAccessModifier #12652

Open
Earlopain opened this issue Jan 24, 2024 · 1 comment
Open

Incorrect example code for Lint/UselessAccessModifier #12652

Earlopain opened this issue Jan 24, 2024 · 1 comment

Comments

@Earlopain
Copy link
Contributor

# @example MethodCreatingMethods: delegate
# # Lint/UselessAccessModifier:
# # MethodCreatingMethods:
# # - delegate
#
# # good
# require 'active_support/core_ext/module/delegation'
# class Foo
# # this is not redundant because `delegate` creates methods
# private
#
# delegate :method_a, to: :method_b
# end

This says that the Rails method delegate respects access modifiers, however that doesn't seem to be the case:

require "active_support/core_ext/module/delegation"
require "ostruct"
class Bar
  def qux
    OpenStruct.new(baz: "bar")
  end
end

class Foo < Bar
  private
  delegate :baz, to: :qux # private: true is what you'd want
end

puts Foo.new.baz # works just fine

This is further supported by delegate having a keyword argument called private to achieve the desired behaviour: https://api.rubyonrails.org/classes/Module.html#method-i-delegate

I would change docs myself but can't actually think of a usecase for this config value.

@Earlopain Earlopain added the bug label Jan 24, 2024
@vlad-pisanov
Copy link

The delegate example is definitely wrong.

MethodCreatingMethods is for things like define_method that are aware of their current visibility. A somewhat contrived example would be:

class C
  private # wrongly flagged as Lint/UselessAccessModifier: Useless private access modifier
  
  send :define_method, :foo, -> { 'E621' } # `private` is respected when `foo` is defined
end

C.new.foo
# private method `foo' called for #<C:0x00007fa1770405b8> (NoMethodError)

@koic koic added documentation and removed bug labels Feb 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants