Skip to content

Commit

Permalink
Merge pull request #10101 from dvandersluis/issue/10096
Browse files Browse the repository at this point in the history
[Fix #10096] Fix `Lint/AmbiguousOperatorPrecedence` with `and`/`or` operators
  • Loading branch information
koic committed Sep 20, 2021
2 parents 28ae02a + 1c20b60 commit 6107f79
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 6107f79

Please sign in to comment.