From 78d32832c9720fe29dc1b6467183c240428cedf7 Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Sat, 15 Feb 2020 16:46:39 +0900 Subject: [PATCH] [Fix #7716] Fix an infinite loop error for `Style/TernaryParentheses` Fix #7716. This PR fixes an infinite loop error for `Style/TernaryParentheses` with `Style/RedundantParentheses` when using `EnforcedStyle: require_parentheses_when_complex`. ```ruby # example.rb !foo.nil? ? 1 : 2 ``` ```yaml # .rubocop.yml Style/TernaryParentheses: EnforcedStyle: require_parentheses_when_complex ``` First, auto-corrected by `Style/TernaryParentheses` (`EnforcedStyle: require_parentheses_when_complex`). ```console % bundle exec rubocop -a --only Style/TernaryParentheses Offenses: example.rb:2:1: C: [Corrected] Style/TernaryParentheses: Use parentheses for ternary expressions with complex conditions. !foo.nil? ? 1 : 2 ^^^^^^^^^^^^^^^^^ 1 file inspected, 1 offense detected, 1 offense corrected ``` ```diff % git diff example.rb diff --git a/7716/example.rb b/7716/example.rb index ae08c67..31212d8 100644 --- a/7716/example.rb +++ b/7716/example.rb @@ -1,2 +1,2 @@ # example.rb -!foo.nil? ? 1 : 2 +(!foo.nil?) ? 1 : 2 ``` Next, auto-corrected by `Style/RedundantParentheses`. ```console % bundle exec rubocop -a --only Style/RedundantParentheses Offenses: example.rb:2:1: C: [Corrected] Style/RedundantParentheses: Don't use parentheses around an unary operation. (!foo.nil?) ? 1 : 2 ^^^^^^^^^^^ 1 file inspected, 1 offense detected, 1 offense corrected ``` This will return to the original code. ```diff % git diff example.rb diff --git a/7716/example.rb b/7716/example.rb index 31212d8..ae08c67 100644 --- a/7716/example.rb +++ b/7716/example.rb @@ -1,2 +1,2 @@ # example.rb -(!foo.nil?) ? 1 : 2 +!foo.nil? ? 1 : 2 ``` That caused the infinite loop in `Style/TernaryParentheses` (`EnforcedStyle: require_parentheses_when_complex`) and `Style/RedundantParentheses`. With this PR, `Style/TernaryParentheses` cop makes aware of `Style/RedundantParentheses` cop when setting `EnforcedStyle: require_parentheses_when_complex`. This does the same thing as `EnforcedStyle: require_parentheses` for infinite loop error. --- CHANGELOG.md | 1 + lib/rubocop/cop/style/ternary_parentheses.rb | 2 +- .../cop/style/ternary_parentheses_spec.rb | 21 ++++++++++++------- 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1fed20fa3df..398e9904f07 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,7 @@ * [#7647](https://github.com/rubocop-hq/rubocop/issues/7647): Fix an `undefined method on_numblock` error when using Ruby 2.7's numbered parameters. ([@hanachin][]) * [#7675](https://github.com/rubocop-hq/rubocop/issues/7675): Fix a false negative for `Layout/SpaceBeforeFirstArg` when a vertical argument positions are aligned. ([@koic][]) * [#7688](https://github.com/rubocop-hq/rubocop/issues/7688): Fix a bug in `Style/MethodCallWithArgsParentheses` that made `--auto-gen-config` crash. ([@buehmann][]) +* [#7203](https://github.com/rubocop-hq/rubocop/issues/7203): Fix an infinite loop error for `Style/TernaryParentheses` with `Style/RedundantParentheses` when using `EnforcedStyle: require_parentheses_when_complex`. ([@koic][]) ### Changes diff --git a/lib/rubocop/cop/style/ternary_parentheses.rb b/lib/rubocop/cop/style/ternary_parentheses.rb index 4de20aa66e2..3d78db2c136 100644 --- a/lib/rubocop/cop/style/ternary_parentheses.rb +++ b/lib/rubocop/cop/style/ternary_parentheses.rb @@ -162,7 +162,7 @@ def parenthesized?(node) # `RedundantParentheses` cop is enabled, it will cause an infinite loop # as they compete to add and remove the parentheses respectively. def infinite_loop? - require_parentheses? && + (require_parentheses? || require_parentheses_when_complex?) && redundant_parentheses_enabled? end diff --git a/spec/rubocop/cop/style/ternary_parentheses_spec.rb b/spec/rubocop/cop/style/ternary_parentheses_spec.rb index b51d7990790..bd911b6bdae 100644 --- a/spec/rubocop/cop/style/ternary_parentheses_spec.rb +++ b/spec/rubocop/cop/style/ternary_parentheses_spec.rb @@ -414,14 +414,21 @@ context 'when `RedundantParenthesis` would cause an infinite loop' do let(:redundant_parens_enabled) { true } - let(:cop_config) do - { - 'EnforcedStyle' => 'require_parentheses', - 'SupportedStyles' => %w[require_parentheses require_no_parentheses] - } + + context 'when `EnforcedStyle: require_parentheses`' do + let(:cop_config) do + { 'EnforcedStyle' => 'require_parentheses' } + end + + it_behaves_like 'code without offense', 'foo = bar? ? a : b' end - it_behaves_like 'code without offense', - 'foo = bar? ? a : b' + context 'when `EnforcedStyle: require_parentheses_when_complex`' do + let(:cop_config) do + { 'EnforcedStyle' => 'require_parentheses_when_complex' } + end + + it_behaves_like 'code without offense', '!condition.nil? ? foo : bar' + end end end