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

Fix a false positive for Style/Aliascop when alias in a method def #11014

Merged
merged 1 commit into from Dec 22, 2022

Conversation

ydah
Copy link
Member

@ydah ydah commented Sep 24, 2022

The following cases will result in a NoMethodError if replaced with alias_method.
Therefore, it is correct to regard it as a violation and not to replace it.

def foo
  alias :ala :bala
end

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.
  • [-] Commit message starts with [Fix #issue-number] (if the related issue exists).
  • 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.

def foo
alias_method :ala, :bala
end
RUBY
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks strange and like a bug. I think NoMethodError is not expected. WDYT?

def foo
  alias :to_string :to_s
end

foo
p 42.to_string # => "#<Integer:0x0000000000000055>"

def bar
  alias_method :to_str, :to_s # => undefined method `alias_method' for main:Object (NoMethodError)
end

bar
p 42.to_str

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ydah ping :-)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed it is. I think it is better to fix it. Can we include the bug fix in this PR?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. This wrong behavior spec cannot be merged. Please update this PR.

@bbatsov
Copy link
Collaborator

bbatsov commented Nov 1, 2022

@ydah Ping :-)

The following cases will result in a NoMethodError
if replaced with alias_method.
Therefore, it is correct to regard it
as a violation and not to replace it.

```ruby
def foo
  alias :ala :bala
end
```
@ydah ydah force-pushed the enhance_doc_for_style_alias branch from 8a0e605 to 05029d9 Compare November 1, 2022 09:38
@ydah ydah changed the title Add test code and documentation for Style/Aliascop Fix a false positive for Style/Aliascop when alias in a method def Nov 1, 2022
@ydah
Copy link
Member Author

ydah commented Nov 1, 2022

I updated this PR. Thank you so much.

@bbatsov bbatsov merged commit 46ffe4f into rubocop:master Dec 22, 2022
@bbatsov
Copy link
Collaborator

bbatsov commented Dec 22, 2022

Thanks!

@ydah ydah deleted the enhance_doc_for_style_alias branch December 22, 2022 07:55
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.

None yet

3 participants