rubocop-performance
rubocop-performance copied to clipboard
Rubocop suggests using same casecmp code even with the logic flipped
Expected behavior
When comparing two case-insensitive strings with != or ==, rubocop suggests using less readable casecmp('...').zero? for performance reason.
There are two problems.
-
First of all, if there's so tight performance concern, I'd not use zero?, I'd instead use
== 0
, which is much faster (and more readable to all programmers). -
The second problem is that it suggests the same code no matter if the condition is flipped. For example:
Use input.strip.casecmp('something').zero? instead of input.strip.downcase == 'something'.
2nd case where I just flipped the logic with !=
:
Use input.strip.casecmp('something').zero? instead of input.strip.downcase != 'something'.
In inverted case (2nd), it suggest the exact same code as the 1st case.
Steps to reproduce the problem
The error will be raised by a code like this, even if it's inverted:
some_string.strip.downcase == 'something'
RuboCop version
$ rubocop -V
1.32.0 (using Parser 3.1.2.0, rubocop-ast 1.19.1, running on ruby 2.7.2 x86_64-linux)
- rubocop-performance 1.14.3
- rubocop-rails 2.15.2
- rubocop-rspec 2.12.1
- rubocop-thread_safety 0.4.4