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

False positive of Style/IdenticalConditionalBranches when conditional mutates duplicate line #12249

Closed
martinemde opened this issue Oct 7, 2023 · 0 comments · Fixed by #12251
Labels

Comments

@martinemde
Copy link
Contributor

martinemde commented Oct 7, 2023

Expected behavior

When the conditional mutates the variable used in the duplication, the conditional should not be considered a violation of cop Style/IdenticalConditionalBranches.

Actual behavior

This is identified as a duplicate branch, but because the duplicated line's behavior is modified by the conditional, it should not be caught by the cop, similar to #9982 where the conditional checks x == 0 before x += 1.

Steps to reproduce the problem

Extracted from Bundler, this removes a leading - and performs a different action depending on if the - existed.

if version.delete_prefix!("-")
  version = version.split("-", 2).unshift(name)
  versions_by_name[name].delete(version)
else
  version = version.split("-", 2).unshift(name)
  versions_by_name[name] << version
end

The unsafe auto-correct is as follows, which is wrong:

version = version.split("-", 2).unshift(name)
if version.delete_prefix!("-")
  versions_by_name[name].delete(version)
else
  versions_by_name[name] << version
end

The version I ended up with that avoids this issue is in this PR.

RuboCop version

1.56.3

@koic koic added the bug label Oct 8, 2023
koic added a commit to koic/rubocop that referenced this issue Oct 8, 2023
…ranches`

Fixes rubocop#12249.

This PR fixes a false positive `Style/IdenticalConditionalBranches`
when `if`..`else` with identical leading lines and assign to condition value.
bbatsov pushed a commit that referenced this issue Oct 10, 2023
Fixes #12249.

This PR fixes a false positive `Style/IdenticalConditionalBranches`
when `if`..`else` with identical leading lines and assign to condition value.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants