Skip to content

Commit

Permalink
[Fix #7165] Fix an error for Style/ConditionalAssignment
Browse files Browse the repository at this point in the history
Fixes #7165.

This PR fixes an auto-correct error for `Style/ConditionalAssignment` when without `else` branch'.

This error occurs when `EnforcedStyle: assign_inside_condition`.

```yaml
# .rubocop.yml
Style/ConditionalAssignment:
  EnforcedStyle: assign_inside_condition
```

```ruby
# example.rb
var = if foo
        bar
      elsif baz
        qux
      end
```

```console
% rubocop -a --only Style/ConditionalAssignment
Inspecting 1 file

0 files inspected, no offenses detected
undefined method `column' for nil:NilClass
/Users/koic/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/rubocop-0.71.0/lib/rubocop/cop/style/conditional_assignment.rb:600:in
`move_branch_'
/Users/koic/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/rubocop-0.71.0/lib/rubocop/cop/style/conditional_assignment.rb:576:in
`block (2 lev'
/Users/koic/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/rubocop-0.71.0/lib/rubocop/cop/style/conditional_assignment.rb:575:in
`each'
/Users/koic/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/rubocop-0.71.0/lib/rubocop/cop/style/conditional_assignment.rb:575:in
`block in mov'
/Users/koic/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/rubocop-0.71.0/lib/rubocop/cop/corrector.rb:64:in
`block (2 levels) in rewrite'
/Users/koic/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/parser-2.6.3.0/lib/parser/source/tree_rewriter.rb:220:in
`transaction'
/Users/koic/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/rubocop-0.71.0/lib/rubocop/cop/corrector.rb:63:in
`block in rewrite'
```
  • Loading branch information
koic authored and bbatsov committed Jun 25, 2019
1 parent 248e921 commit c595654
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
* [#7118](https://github.com/rubocop-hq/rubocop/pull/7118): Fix `Style/WordArray` with `encoding: binary` magic comment and non-ASCII string. ([@pocke][])
* [#7159](https://github.com/rubocop-hq/rubocop/issues/7159): Fix an error for `Lint/DuplicatedKey` when using endless range. ([@koic][])
* [#7151](https://github.com/rubocop-hq/rubocop/issues/7151): Fix `Style/WordArray` to also consider words containing hyphens. ([@fwitzke][])
* [#7165](https://github.com/rubocop-hq/rubocop/issues/7165): Fix an auto-correct error for `Style/ConditionalAssignment` when without `else` branch'. ([@koic][])

### Changes

Expand Down
3 changes: 2 additions & 1 deletion lib/rubocop/cop/style/conditional_assignment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,8 @@ def move_branch_inside_condition(corrector, branch, condition,

remove_whitespace_in_branches(corrector, branch, condition, column)

branch_else = branch.parent.loc.else
return unless (branch_else = branch.parent.loc.else)

corrector.remove_preceding(branch_else, branch_else.column - column)
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -829,6 +829,25 @@
RUBY
end
end

it 'corrects assignment when without `else` branch' do
expect_offense(<<~RUBY)
var = if foo
^^^^^^^^^^^^ Assign variables inside of conditionals
bar
elsif baz
qux
end
RUBY

expect_correction(<<~RUBY)
if foo
var = bar
elsif baz
var = qux
end
RUBY
end
end

context 'SingleLineConditionsOnly false' do
Expand Down

0 comments on commit c595654

Please sign in to comment.