Open miry opened 8 months ago
This is just Style/NumericPredicate
, is it not? It's configured for predicates by default so it would say that they will flip-flop between each other if not handled specifically. I'm also not sure if its worth the effort to duplicate a cop for what should basically be a different config value.
@Earlopain Are there any established practices for handling conflicts between rubocop cops and performance cops? I wonder, if there is a way to check the RuboCop configuration to ensure that the performance-related cops are enabled.
Performance::NumericPredicate cop identifies places where numeric uses predicates like
positive?
,negative?
and for some caseszero?
should be converted to compare operator.The
Performance::NumericPredicate
cop is added to identify instances where numeric predicates such aspositive?
,negative?
, and occasionallyzero?
should be replaced with comparison operators for improved efficiency.Predicates incur a performance overhead by executing a method before comparison. A small benchmark comparison between using a comparison operator (
> 0
) andpositive?
illustrates the performance difference:Benchmark results on Ruby 3.3.0 (with YJIT) indicate a significant performance gain when using the comparison operator:
This cop is unsafe because it cannot be guaranteed that the receiver is Number and could be noisy.
Before submitting the PR make sure the following are checked:
[Fix #issue-number]
(if the related issue exists).master
(if not - rebase it).bundle exec rake default
. It executes all tests and runs RuboCop on its own code.{change_type}_{change_description}.md
if the new code introduces user-observable changes. See changelog entry format for details.