Skip to content

Commit

Permalink
[Fix rubocop#4352] Fix auto-correct of AndOr with Enumerable accessors
Browse files Browse the repository at this point in the history
  • Loading branch information
rrosenblum committed May 16, 2017
1 parent cebc032 commit 10577b6
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
* [#4327](https://github.com/bbatsov/rubocop/issues/4327): Prevent `Layout/SpaceInsidePercentLiteralDelimiters` from registering offenses on execute-strings. ([@drenmi][])
* [#4371](https://github.com/bbatsov/rubocop/issues/4371): Prevent `Style/MethodName` from complaining about unary operator definitions. ([@drenmi][])
* [#4366](https://github.com/bbatsov/rubocop/issues/4366): Prevent `Performance/RedundantMerge` from blowing up on double splat arguments. ([@drenmi][])
* [#4352](https://github.com/bbatsov/rubocop/issues/4352): Fix the auto-correct of `Style/AndOr` when Enumerable accessors (`[]`) are used. ([@rrosenblum][])

## 0.48.1 (2017-04-03)

Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/style/and_or.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def correct_other(node, corrector)
end

def correctable_send?(node)
!node.parenthesized? && node.arguments?
!node.parenthesized? && !node.method?(:[]) && node.arguments?
end

def whitespace_before_arg(node)
Expand Down
6 changes: 6 additions & 0 deletions spec/rubocop/cop/style/and_or_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,12 @@ def z(a, b)
expect(new_source).to eq('(x = a + b) && (return x)')
end

it 'autocorrects "and" with an Enumerable accessor on either side' do
src = 'foo[:bar] and foo[:baz]'
new_source = autocorrect_source(cop, src)
expect(new_source).to eq('foo[:bar] && foo[:baz]')
end

it 'warns on short-circuit (and)' do
expect_offense(<<-RUBY.strip_indent)
x = a + b and return x
Expand Down

0 comments on commit 10577b6

Please sign in to comment.