KEMBAR78
Fix false positives for `Lint/DuplicateMethods` when the self-alias trick is used by viralpraxis · Pull Request #14300 · rubocop/rubocop · GitHub
Skip to content

Conversation

viralpraxis
Copy link
Contributor

@viralpraxis viralpraxis commented Jun 18, 2025

When there's a self-alias, there's no need to register an offense:

class a
  alias foo foo
  # or alias_method :foo, :foo
  def foo; end
end

Using the alias-self trick indicates that developer is trying to avoid "method redefinition" warning.


Before submitting the PR make sure the following are checked:

  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • Wrote good commit messages.
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Ran bundle exec rake default. It executes all tests and runs RuboCop on its own code.
  • Added an entry (file) to the changelog folder named {change_type}_{change_description}.md if the new code introduces user-observable changes. See changelog entry format for details.

@viralpraxis viralpraxis force-pushed the fix-lint-duplicate-methods-false-positives-with-self-aliases branch from 197fcf2 to 9ecaf38 Compare June 18, 2025 08:57
@bbatsov
Copy link
Collaborator

bbatsov commented Jun 18, 2025

I've never heard of this trick, but if it's something common probably the cop's docs should also be expanded to mention explicitly that we're allowing this and why.

@viralpraxis viralpraxis force-pushed the fix-lint-duplicate-methods-false-positives-with-self-aliases branch from 9ecaf38 to e605184 Compare June 18, 2025 09:47
@viralpraxis
Copy link
Contributor Author

viralpraxis commented Jun 18, 2025

I've never heard of this trick, but if it's something common probably the cop's docs should also be expanded to mention explicitly that we're allowing this and why.

Done.

You can learn a bit more here: https://bugs.ruby-lang.org/issues/13574
It's common enough to be included in the ActiveSupport: https://github.com/rails/rails/blob/19eebf6d33dd15a0172e3ed2481bec57a89a2404/activesupport/lib/active_support/core_ext/module/redefine_method.rb#L7

@bbatsov
Copy link
Collaborator

bbatsov commented Jun 20, 2025

Note to self - an explanation of the need for this https://bugs.ruby-lang.org/issues/13574

Seems it's a bit controversial, so I wonder if this should be behind a flag. My main concern is that you might want to suppress the warnings or you might have made a mistake in your alias and it's kind of hard to guess.

@bbatsov
Copy link
Collaborator

bbatsov commented Jun 27, 2025

@rubocop/rubocop-core Any opinions about this suggestion?

@Earlopain
Copy link
Contributor

It seems fine to me. It's sad that it needs such an unintuitive workaround (I also don't think that it is documented anywhere) but I also have encountered such code in different places to achieve the same thing. At least with rails you have access to a descriptive name for it, wish ruby would do something like that too.

Personally I find it very unlikely to write code like this by accident and not fix it if you intended to do a "real" alias. If you expect some method to be available after, you should notice pretty quickly that it actually isn't.

@viralpraxis viralpraxis force-pushed the fix-lint-duplicate-methods-false-positives-with-self-aliases branch from e605184 to b275d5d Compare June 27, 2025 13:02
@viralpraxis viralpraxis force-pushed the fix-lint-duplicate-methods-false-positives-with-self-aliases branch 2 times, most recently from c14a846 to 26e388d Compare July 3, 2025 17:12
…ck is used

When there's a self alias, there's no need to register an offense:

```ruby
class a
  alias foo foo
  # or alias_method :foo, :foo
  def foo; end
```

Using the alias-self trick indicates that developer is trying to avoid
"method redefinition" warning.
@viralpraxis viralpraxis force-pushed the fix-lint-duplicate-methods-false-positives-with-self-aliases branch from 26e388d to a8e6ef9 Compare July 3, 2025 17:14
@bbatsov bbatsov merged commit 2508d94 into rubocop:master Jul 8, 2025
24 checks passed
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 this pull request may close these issues.

3 participants