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 #10325] Enhance Style/RedundantCondition for the case that variable assignments in each branch #10593

Conversation

nobuyo
Copy link
Contributor

@nobuyo nobuyo commented Apr 30, 2022

Fixes #10325.

The example using nil? given in the issue is not supported here, as it would destroy the code whose intent is to execute else_branch when value == false.

# not supported
def lock_retrier=(value)
  if value.nil?
    @lock_retrier = NullLockRetrier.new
  else
    @lock_retrier = value
  end
end

# supported
def lock_retrier=(value)
  if value
    @lock_retrier = NullLockRetrier.new
  else
    @lock_retrier = value
  end
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.

… the case that variable assignments in each branch
@nobuyo nobuyo force-pushed the enhance-redundant-condition-if-branches-have-assighment branch from d8133a2 to 96d0a99 Compare April 30, 2022 14:41
@nobuyo nobuyo marked this pull request as draft April 30, 2022 16:21
@nobuyo
Copy link
Contributor Author

nobuyo commented Apr 30, 2022

Should the following patterns be covered as well?
I have a feeling that it may be overkill.

if foo
  test.bar foo
else
  test.bar 'baz'
end
# => test.bar foo || 'baz'

if foo
  test.bar = foo
else
  test.bar = 'baz'
end
# => test.bar = foo || 'baz'

@nobuyo nobuyo marked this pull request as ready for review May 1, 2022 01:56
@nobuyo
Copy link
Contributor Author

nobuyo commented May 1, 2022

@rubocop/rubocop-core I pushed af27f01 with the split as I dared. If you give me your thoughts, I'd like to squash or revert it.

@bbatsov bbatsov merged commit 1b6bda4 into rubocop:master May 6, 2022
@bbatsov
Copy link
Collaborator

bbatsov commented May 6, 2022

The changes look reasonable to me. Thanks!

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.

New cop to replace conditional assignment with or-assignment
2 participants