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

Add new Style/SwapValues cop #8934

Merged
merged 1 commit into from Oct 26, 2020
Merged

Conversation

fatkodima
Copy link
Contributor

This cop enforces the use of shorthand-style swapping of 2 variables.

# bad
tmp = x
x = y
y = tmp

# good
x, y = y, x

This will be mostly useful for newcomers from other languages into ruby, because when you used it once you will always remember about its existence.

@bbatsov
Copy link
Collaborator

bbatsov commented Oct 25, 2020

Great idea! Seems that we should also add this to the style guide.

|===

This cop enforces the use of shorthand-style swapping of 2 variables.
It is marked as unsafe, because while autocorrecting it can erroneously remove
Copy link
Collaborator

Choose a reason for hiding this comment

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

The wording here is a bit misleading, as the cop is safe and only the auto-correct is unsafe.

@fatkodima
Copy link
Contributor Author

Added link to the style guide (rubocop/ruby-style-guide#853) and changed wording.

@bbatsov bbatsov merged commit 1a35844 into rubocop:master Oct 26, 2020
@bbatsov
Copy link
Collaborator

bbatsov commented Oct 26, 2020

Thanks!

@pvdb
Copy link

pvdb commented Feb 2, 2022

There's an issue #10394 with this cop that I just ran into.

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