-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Closed
Labels
Description
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