Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Performance::NumericPredicate cop #440

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/new_add_performancemumericpredicate_cop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#440](https://github.com/rubocop/rubocop-performance/pull/440): Add Performance::NumericPredicate cop. ([@miry][])
6 changes: 6 additions & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,12 @@ Performance/MethodObjectAsBlock:
Enabled: pending
VersionAdded: '1.9'

Performance/NumericPredicate:
Description: 'Use compare operator instead of `Numeric#positive?`, `Numeric#negative?`, or `Numeric#zero?`.'
Enabled: pending
Safe: false
VersionAdded: '<<next>>'

Performance/OpenStruct:
Description: 'Use `Struct` instead of `OpenStruct`.'
Enabled: false
Expand Down
61 changes: 61 additions & 0 deletions lib/rubocop/cop/performance/numeric_predicate.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# frozen_string_literal: true

module RuboCop
module Cop
module Performance
# Identifies places where numeric uses predicates `positive?`, and `negative?` should be
# converted to compare operator.
#
# @safety
# This cop is unsafe because it cannot be guaranteed that the receiver
# defines the predicates or can be compared to a number, which may lead
# to a false positive for non-standard classes.
#
# @example
# # bad
# 1.positive?
# 1.43.negative?
# -4.zero?
#
# # good
# 1 > 0
# 1.43 < 0.0
# -4 == 0
#
class NumericPredicate < Base
extend AutoCorrector

MSG = 'Use compare operator `%<good>s` instead of `%<bad>s`.'
RESTRICT_ON_SEND = %i[positive? zero? negative?].freeze
REPLACEMENTS = { negative?: '<', positive?: '>', zero?: '==' }.freeze

def_node_matcher :num_predicate?, <<~PATTERN
(send $numeric_type? ${:negative? :positive? :zero?})
PATTERN

def_node_matcher :instance_predicate?, <<~PATTERN
(send $!nil? ${:negative? :positive?})
PATTERN

def on_send(node)
return unless num_predicate?(node) || instance_predicate?(node)
return unless node.receiver

good_method = build_good_method(node.receiver, node.method_name)
message = format(MSG, good: good_method, bad: node.source)
add_offense(node, message: message) do |corrector|
corrector.replace(node, good_method)
end
end

private

def build_good_method(receiver, method)
operation = REPLACEMENTS[method]
zero = receiver&.float_type? ? 0.0 : 0
"#{receiver.source} #{operation} #{zero}"
end
end
end
end
end
1 change: 1 addition & 0 deletions lib/rubocop/cop/performance_cops.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
require_relative 'performance/map_compact'
require_relative 'performance/map_method_chain'
require_relative 'performance/method_object_as_block'
require_relative 'performance/numeric_predicate'
require_relative 'performance/open_struct'
require_relative 'performance/range_include'
require_relative 'performance/io_readlines'
Expand Down
88 changes: 88 additions & 0 deletions spec/rubocop/cop/performance/numeric_predicate_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# frozen_string_literal: true

RSpec.describe RuboCop::Cop::Performance::NumericPredicate, :config do
let(:message) { RuboCop::Cop::Performance::NumericPredicate::MSG }

shared_examples 'common functionality' do |method, op|
it 'for integer' do
expect_offense(<<~RUBY, method: method)
1.#{method}
^^^{method} Use compare operator `1 #{op} 0` instead of `1.#{method}`.
RUBY

expect_correction(<<~RUBY)
1 #{op} 0
RUBY
end

it 'for float' do
expect_offense(<<~RUBY, method: method)
1.2.#{method}
^^^^^{method} Use compare operator `1.2 #{op} 0.0` instead of `1.2.#{method}`.
RUBY

expect_correction(<<~RUBY)
1.2 #{op} 0.0
RUBY
end

it 'ignore big decimal' do
next if method == 'zero?'

expect_offense(<<~RUBY, method: method)
BigDecimal('1', 2).#{method}
^^^^^^^^^^^^^^^^^^^^{method} Use compare operator `BigDecimal('1', 2) #{op} 0` instead of `BigDecimal('1', 2).#{method}`.
RUBY

expect_correction(<<~RUBY)
BigDecimal('1', 2) #{op} 0
RUBY
end

it 'for variable' do
next if method == 'zero?'

expect_offense(<<~RUBY, method: method)
foo = 1
foo.#{method}
^^^^^{method} Use compare operator `foo #{op} 0` instead of `foo.#{method}`.
RUBY

expect_correction(<<~RUBY)
foo = 1
foo #{op} 0
RUBY
end

it 'in condition statements' do
next if method == 'zero?'

expect_offense(<<~RUBY, method: method)
foo = 1
if foo.#{method}
^^^^^{method} Use compare operator `foo #{op} 0` instead of `foo.#{method}`.
end
RUBY

expect_correction(<<~RUBY)
foo = 1
if foo #{op} 0
end
RUBY
end

it 'in map statement' do
next if method == 'zero?'

expect_no_offenses(<<~RUBY, method: method)
foo = [1, 2, 3]
if foo.all?(&:#{method})
end
RUBY
end
end

it_behaves_like 'common functionality', 'positive?', '>'
it_behaves_like 'common functionality', 'negative?', '<'
it_behaves_like 'common functionality', 'zero?', '=='
end