diff --git a/changelog/change_mark_unsafe_autocorrect_for_style_identical_conditional_branches.mdb b/changelog/change_mark_unsafe_autocorrect_for_style_identical_conditional_branches.mdb new file mode 100644 index 00000000000..fcfe42759d5 --- /dev/null +++ b/changelog/change_mark_unsafe_autocorrect_for_style_identical_conditional_branches.mdb @@ -0,0 +1 @@ +* [#9985](https://github.com/rubocop/rubocop/pull/9985): Mark `Style/IdenticalConditionalBranches` as unsafe auto-correction. ([@koic][]) diff --git a/config/default.yml b/config/default.yml index c4231cde358..f797bb86da6 100644 --- a/config/default.yml +++ b/config/default.yml @@ -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: '<>' Style/IfInsideElse: Description: 'Finds if nodes inside else, which can be converted to elsif.' diff --git a/lib/rubocop/cop/style/identical_conditional_branches.rb b/lib/rubocop/cop/style/identical_conditional_branches.rb index 0eaf35d3367..bdd43cf01d8 100644 --- a/lib/rubocop/cop/style/identical_conditional_branches.rb +++ b/lib/rubocop/cop/style/identical_conditional_branches.rb @@ -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.