Skip to content

Commit

Permalink
[Fix rubocop#10096] Fix Lint/AmbiguousOperatorPrecedence with and
Browse files Browse the repository at this point in the history
…/`or` operators.
  • Loading branch information
dvandersluis committed Sep 20, 2021
1 parent 28ae02a commit 1c20b60
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog/fix_fix_lintambiguousoperatorprecedence_with.md
@@ -0,0 +1 @@
* [#10096](https://github.com/rubocop/rubocop/issues/10096): Fix `Lint/AmbiguousOperatorPrecedence` with `and`/`or` operators. ([@dvandersluis][])
6 changes: 5 additions & 1 deletion lib/rubocop/cop/lint/ambiguous_operator_precedence.rb
Expand Up @@ -87,7 +87,11 @@ def operator?(node)
end

def greater_precedence?(node1, node2)
precedence(node2) > precedence(node1)
node1_precedence = precedence(node1)
node2_precedence = precedence(node2)
return false unless node1_precedence && node2_precedence

node2_precedence > node1_precedence
end

def operator_name(node)
Expand Down
12 changes: 12 additions & 0 deletions spec/rubocop/cop/lint/ambiguous_operator_precedence_spec.rb
Expand Up @@ -120,4 +120,16 @@
a || (b && (c | d ^ (e & (f << g >> (h + i - (j * k / l % (n ** m)))))))
RUBY
end

it 'allows an operator with `and`' do
expect_no_offenses(<<~RUBY)
array << i and next
RUBY
end

it 'allows an operator with `or`' do
expect_no_offenses(<<~RUBY)
array << i or return
RUBY
end
end

0 comments on commit 1c20b60

Please sign in to comment.