Skip to content

Commit

Permalink
Merge pull request #13081 from fatkodima/zero_length_predicate-false-…
Browse files Browse the repository at this point in the history
…positive

Fix a false positive for `Style/ZeroLengthPredicate` when using safe navigation and non-zero comparison
  • Loading branch information
koic committed Jul 30, 2024
2 parents e1e8875 + 70d8e88 commit 748a568
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 8 deletions.
1 change: 1 addition & 0 deletions changelog/fix_false_positive_zero_length_predicate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#13081](https://github.com/rubocop/rubocop/pull/13081): Fix a false positive for `Style/ZeroLengthPredicate` when using safe navigation and non-zero comparison. ([@fatkodima][])
6 changes: 5 additions & 1 deletion lib/rubocop/cop/style/zero_length_predicate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@ def on_send(node)
check_zero_length_comparison(node)
check_nonzero_length_comparison(node)
end
alias on_csend on_send

def on_csend(node)
check_zero_length_predicate(node)
check_zero_length_comparison(node)
end

private

Expand Down
9 changes: 2 additions & 7 deletions spec/rubocop/cop/style/zero_length_predicate_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -178,14 +178,9 @@
RUBY
end

it 'registers an offense for `array&.length > 0`' do
expect_offense(<<~RUBY)
it 'does not register an offense for `array&.length > 0`' do
expect_no_offenses(<<~RUBY)
[1, 2, 3]&.length > 0
^^^^^^^^^^^^^^^^^^^^^ Use `!empty?` instead of `length > 0`.
RUBY

expect_correction(<<~RUBY)
![1, 2, 3]&.empty?
RUBY
end

Expand Down

0 comments on commit 748a568

Please sign in to comment.