Skip to content

Commit 748a568

Browse files
authored
Merge pull request #13081 from fatkodima/zero_length_predicate-false-positive
Fix a false positive for `Style/ZeroLengthPredicate` when using safe navigation and non-zero comparison
2 parents e1e8875 + 70d8e88 commit 748a568

File tree

3 files changed

+8
-8
lines changed

3 files changed

+8
-8
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
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][])

lib/rubocop/cop/style/zero_length_predicate.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,11 @@ def on_send(node)
4747
check_zero_length_comparison(node)
4848
check_nonzero_length_comparison(node)
4949
end
50-
alias on_csend on_send
50+
51+
def on_csend(node)
52+
check_zero_length_predicate(node)
53+
check_zero_length_comparison(node)
54+
end
5155

5256
private
5357

spec/rubocop/cop/style/zero_length_predicate_spec.rb

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -178,14 +178,9 @@
178178
RUBY
179179
end
180180

181-
it 'registers an offense for `array&.length > 0`' do
182-
expect_offense(<<~RUBY)
181+
it 'does not register an offense for `array&.length > 0`' do
182+
expect_no_offenses(<<~RUBY)
183183
[1, 2, 3]&.length > 0
184-
^^^^^^^^^^^^^^^^^^^^^ Use `!empty?` instead of `length > 0`.
185-
RUBY
186-
187-
expect_correction(<<~RUBY)
188-
![1, 2, 3]&.empty?
189184
RUBY
190185
end
191186

0 commit comments

Comments
 (0)