Navigation Menu

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

Conversation

koic
Copy link
Member

@koic koic commented Feb 15, 2020

Fix #7716.

This PR fixes an infinite loop error for Style/TernaryParentheses with Style/RedundantParentheses when using EnforcedStyle: require_parentheses_when_complex.

# example.rb
!foo.nil? ? 1 : 2
# .rubocop.yml
Style/TernaryParentheses:
  EnforcedStyle: require_parentheses_when_complex

First, auto-corrected by Style/TernaryParentheses (EnforcedStyle: require_parentheses_when_complex).

% 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
% 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.

% 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.

% 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.


Before submitting the PR make sure the following are checked:

  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Added an entry to the Changelog if the new code introduces user-observable changes. See changelog entry format.
  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • Run bundle exec rake default. It executes all tests and RuboCop for itself, and generates the documentation.

…theses`

Fix rubocop#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.
Copy link
Collaborator

@Drenmi Drenmi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@koic koic merged commit 1e905b1 into rubocop:master Feb 16, 2020
@koic koic deleted the fix_an_infinite_loop_error_for_redundant_parentheses branch February 16, 2020 02:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

infinite loop between Style/TernaryParentheses and Style/RedundantParentheses
2 participants