Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix #7716] Fix an infinite loop error for Style/TernaryParentheses #7718

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/style/ternary_parentheses.rb
Expand Up @@ -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

Expand Down
21 changes: 14 additions & 7 deletions spec/rubocop/cop/style/ternary_parentheses_spec.rb
Expand Up @@ -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