From 399809469ee2df6aa53481a55f33471b39e24382 Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Mon, 4 Feb 2019 10:04:28 +0900 Subject: [PATCH] [Fix #6722] Fix an error for `Style/OneLineConditional` Fixes #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. --- CHANGELOG.md | 1 + lib/rubocop/cop/style/one_line_conditional.rb | 2 ++ spec/rubocop/cop/style/one_line_conditional_spec.rb | 7 +++++++ 3 files changed, 10 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index c1e3971d35a..ba29ba5995e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ * [#6668](https://github.com/rubocop-hq/rubocop/issues/6668): Fix autocorrection for `Style/UnneededCondition` when conditional has the `unless` form. ([@mvz][]) * [#6382](https://github.com/rubocop-hq/rubocop/issues/6382): Fix `Layout/IndentationWidth` with `Layout/EndAlignment` set to start_of_line. ([@dischorde][], [@siegfault][], [@mhelmetag][]) * [#6710](https://github.com/rubocop-hq/rubocop/issues/6710): Fix `Naming/MemoizedInstanceVariableName` on method starts with underscore. ([@pocke][]) +* [#6722](https://github.com/rubocop-hq/rubocop/issues/6722): Fix an error for `Style/OneLineConditional` when `then` branch has no body. ([@koic][]) ### Changes diff --git a/lib/rubocop/cop/style/one_line_conditional.rb b/lib/rubocop/cop/style/one_line_conditional.rb index 51751979734..1abda984106 100644 --- a/lib/rubocop/cop/style/one_line_conditional.rb +++ b/lib/rubocop/cop/style/one_line_conditional.rb @@ -67,6 +67,8 @@ def to_ternary(node) end def expr_replacement(node) + return 'nil' if node.nil? + requires_parentheses?(node) ? "(#{node.source})" : node.source end diff --git a/spec/rubocop/cop/style/one_line_conditional_spec.rb b/spec/rubocop/cop/style/one_line_conditional_spec.rb index 34b066c21e1..34012ca227d 100644 --- a/spec/rubocop/cop/style/one_line_conditional_spec.rb +++ b/spec/rubocop/cop/style/one_line_conditional_spec.rb @@ -38,6 +38,13 @@ end end + context 'one line if/then/else/end when `then` branch has no body' do + let(:source) { 'if cond then else dont end' } + + include_examples 'offense', 'if' + include_examples 'autocorrect', 'cond ? nil : dont' + end + context 'one line if/then/end' do let(:source) { 'if cond then run end' }