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

Lint/DuplicateMethods false positive when using alias_method to change/change back a method #11273

Closed
tjschuck opened this issue Dec 13, 2022 · 0 comments · Fixed by #11274
Closed
Labels

Comments

@tjschuck
Copy link
Contributor

This is something we sometimes do as a test helper method to ensure some sort of behavior out of a method in the class to be tested. Imagine, for example, we wanted save in the tested class to always raise an error when called, but we also don't want it to stay modified:

module FooTest
  def make_save_always_fail
    Foo.class_eval do
      def failed_save
        raise
      end
      alias_method :original_save, :save
      alias_method :save, :failed_save
    end

    yield
  ensure
    Foo.class_eval do
      alias_method :save, :original_save
    end
  end
end

If you call rubocop --only Lint/DuplicateMethods on that snippet, you get a false positive for the "second" definition of save inside the ensure block.

https://wasabigeek.com/blog/practical-metaprogramming-in-ruby-minitest-mock/ outlines a similar form of this.


Expected behavior

Should allow multiple alias_method definitions of the same name if they happen in different scopes.

Actual behavior

See above.

Steps to reproduce the problem

See example snippet and command above.

RuboCop version

1.40.0 (using Parser 3.1.3.0, rubocop-ast 1.24.0, running on ruby 3.0.4) [x86_64-darwin21]
@koic koic added the bug label Dec 13, 2022
koic added a commit to koic/rubocop that referenced this issue Dec 13, 2022
Fixes rubocop#11273.

This PR fixes a false positive for `Lint/DuplicateMethods`
when there are same `alias_method` name outside `rescue` or `ensure` scopes.
bbatsov pushed a commit that referenced this issue Dec 13, 2022
Fixes #11273.

This PR fixes a false positive for `Lint/DuplicateMethods`
when there are same `alias_method` name outside `rescue` or `ensure` scopes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants