Skip to content

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

@martinemde

Description

@martinemde

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy