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 #6722] Fix an error for Style/OneLineConditional #6727

Merged
merged 1 commit into from Feb 4, 2019

Conversation

koic
Copy link
Member

@koic koic commented Feb 4, 2019

Fixes #6722.

This PR fixes an error for Style/OneLineConditional when then branch has no body. The following is an error case.

# example.rb
if cond then else dont end

The following is a reproduction step.

% rubocop -v
0.63.1

% rubocop -a example.rb --only Style/OneLineConditional
Inspecting 1 file

0 files inspected, no offenses detected
undefined method `type' for nil:NilClass
/Users/koic/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/rubocop-.63.1/lib/rubocop/cop/style/one_line_conditional.rb:74:in `requires_parentheses?'
/Users/koic/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/rubocop-.63.1/lib/rubocop/cop/style/one_line_conditional.rb:70:in `expr_replacement'
/Users/koic/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/rubocop-.63.1/lib/rubocop/cop/style/one_line_conditional.rb:65:in `to_ternary'

(snip)

This PR auto-corrected to the following code.

cond ? nil : dont

Actually the above code is the same behavior as unless modifier. But this PR aims auto-corrected to an equivalent code without causing an 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.

Fixes rubocop#6722.

This PR fixes an error for `Style/OneLineConditional` when
`then` branch has no body. The following is an error case.

```ruby
# example.rb
if cond then else dont end
```

The following is a reproduction step.

```console
% rubocop -v
0.63.1

% rubocop -a example.rb --only Style/OneLineConditional
Inspecting 1 file

0 files inspected, no offenses detected
undefined method `type' for nil:NilClass
/Users/koic/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/rubocop-0.63.1/lib/rubocop/cop/style/one_line_conditional.rb:74:in
`requires_parentheses?'
/Users/koic/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/rubocop-0.63.1/lib/rubocop/cop/style/one_line_conditional.rb:70:in
`expr_replacement'
/Users/koic/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/rubocop-0.63.1/lib/rubocop/cop/style/one_line_conditional.rb:65:in
`to_ternary'

(snip)
```

This PR auto-corrected to the following code.

```ruby
cond ? nil : dont
```

Actually the above code is the same behavior as `unless` modifier.
But this PR aims auto-corrected to an equivalent code without causing an error.
@koic koic mentioned this pull request Feb 4, 2019
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.

🚀

@Drenmi Drenmi merged commit f288c2c into rubocop:master Feb 4, 2019
@koic koic deleted the fix_error_for_one_line_conditional branch February 4, 2019 02:24
@Drenmi
Copy link
Collaborator

Drenmi commented Feb 4, 2019

Actually the above code is the same behavior as unless modifier. But this PR aims auto-corrected to an equivalent code without causing an error.

Yep. Sounds like another cop should be picking this up. 🤔

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.

0 files inspected?
2 participants