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 autocorrect for Lint/AssignmentInCondition #11211

Merged
merged 2 commits into from
Nov 30, 2022

Conversation

r7kamura
Copy link
Contributor

@r7kamura r7kamura commented Nov 29, 2022

I think adding support for unsafe autocorrection to Lint/AssignmentInCondition would be more useful in some cases. Now that SafeAutoCorrect: false allows us to indicate to users that it is unsafe, I don't think we will have much trouble because of this support.

I decided to replace the code as assignment by default in this autocorrection because it does not change the behavior of the current code. If there seem to be many use cases where the behavior of autocorrection needs to be changed, we can add such configuration in this Pull Request or the future change. (In fact, is there any Cop that allows the autocorrection behavior to be switched by configuration in that way?)

Depending on the policy, this type of autocorrection support may not be acceptable in RuboCop. In which case, please do not hesitate to close this Pull Request.


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.

@r7kamura
Copy link
Contributor Author

As background, I had to migrate a bit large old codebase from rubocop 0.44 to 1.39. I thought it would be easy to complete with autocorrection because the tests were well written, but this cop did not support autocorrection, so I had to manually rewrite a lot of such ambiguous assignment code, which was very complicated task.
In other words, I am saying that there are cases where it is useful enough to have it provided, even if it is not a perfect autocorrection.

@koic
Copy link
Member

koic commented Nov 29, 2022

I think any configuration is not required for this autocorrection. So this unsafe autocorrection approach would be better.

Co-authored-by: Koichi ITO <koic.ito@gmail.com>
@bbatsov bbatsov merged commit 61db857 into rubocop:master Nov 30, 2022
@bbatsov
Copy link
Collaborator

bbatsov commented Nov 30, 2022

I like the proposal. Thanks!

@r7kamura r7kamura deleted the feature/assignment-in-condition branch November 30, 2022 07:33
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