Skip to content

Commit

Permalink
Mark Style/IdenticalConditionalBranches as unsafe auto-correction
Browse files Browse the repository at this point in the history
Follow up to #9982 (comment)

This PR marks `Style/IdenticalConditionalBranches` as unsafe auto-correction.

`Style/IdenticalConditionalBranches` cop is marked unsafe auto-correction
as the order of method calls must be guaranteed in the following case:

```ruby
if method_that_modifies_global_state # 1
  method_that_relies_on_global_state # 2
  foo                                # 3
else
  method_that_relies_on_global_state # 2
  bar                                # 3
end
```

In such a case, auto-correction may change the invocation order.
  • Loading branch information
koic committed Aug 6, 2021
1 parent 4460b6d commit 6059c6d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
@@ -0,0 +1 @@
* [#9985](https://github.com/rubocop/rubocop/pull/9985): Mark `Style/IdenticalConditionalBranches` as unsafe auto-correction. ([@koic][])
3 changes: 2 additions & 1 deletion config/default.yml
Expand Up @@ -3604,8 +3604,9 @@ Style/IdenticalConditionalBranches:
line at the end of each branch, which can validly be moved
out of the conditional.
Enabled: true
SafeAutoCorrect: false
VersionAdded: '0.36'
VersionChanged: '1.16'
VersionChanged: '<<next>>'

Style/IfInsideElse:
Description: 'Finds if nodes inside else, which can be converted to elsif.'
Expand Down
16 changes: 16 additions & 0 deletions lib/rubocop/cop/style/identical_conditional_branches.rb
Expand Up @@ -7,6 +7,22 @@ module Style
# each branch of a conditional expression. Such expressions should normally
# be placed outside the conditional expression - before or after it.
#
# This cop is marked unsafe auto-correction as the order of method invocations
# must be guaranteed in the following case:
#
# [source,ruby]
# ----
# if method_that_modifies_global_state # 1
# method_that_relies_on_global_state # 2
# foo # 3
# else
# method_that_relies_on_global_state # 2
# bar # 3
# end
# ----
#
# In such a case, auto-correction may change the invocation order.
#
# NOTE: The cop is poorly named and some people might think that it actually
# checks for duplicated conditional branches. The name will probably be changed
# in a future major RuboCop release.
Expand Down

0 comments on commit 6059c6d

Please sign in to comment.