Skip to content

Commit

Permalink
Add #to_d support to BigDecimalWithNumericArgument
Browse files Browse the repository at this point in the history
The [`bigdecimal/util`](https://github.com/ruby/bigdecimal/blob/v3.0.2/lib/bigdecimal/util.rb)
library adds `#to_d` to several numeric types, where

```ruby
numeric.to_d(*args)
```

is equivalent to

```ruby
BigDecimal(numeric, *args)
```

and the performance improvement of `BigDecimalWithNumericArgument`
therefore equally applies here.
  • Loading branch information
leoarnold committed Nov 13, 2021
1 parent 51409ce commit b13c0ca
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -312,3 +312,4 @@
[@ghiculescu]: https://github.com/ghiculescu
[@mfbmina]: https://github.com/mfbmina
[@mvz]: https://github.com/mvz
[@leoarnold]: https://github.com/leoarnold
Empty file added changelog/.gitkeep
Empty file.
1 change: 1 addition & 0 deletions changelog/new_add_to_d_to_BigDecimalWithNumericArgument.md
@@ -0,0 +1 @@
* [#269](https://github.com/rubocop/rubocop-performance/pull/269): Add `#to_d` support to `BigDecimalWithNumericArgument`. ([@leoarnold][])
2 changes: 1 addition & 1 deletion config/default.yml
Expand Up @@ -17,7 +17,7 @@ Performance/ArraySemiInfiniteRangeSlice:
VersionAdded: '1.9'

Performance/BigDecimalWithNumericArgument:
Description: 'Convert numeric argument to string before passing to BigDecimal.'
Description: 'Convert numeric literal to string and pass it to `BigDecimal`.'
Enabled: 'pending'
VersionAdded: '1.7'

Expand Down
45 changes: 36 additions & 9 deletions lib/rubocop/cop/performance/big_decimal_with_numeric_argument.rb
Expand Up @@ -10,35 +10,62 @@ module Performance
# @example
# # bad
# BigDecimal(1, 2)
# BigDecimal(1.2, 3, exception: true)
# 1.to_d(2)
# BigDecimal(1.2, exception: true)
# 1.2.to_d(exception: true)
#
# # good
# BigDecimal('1', 2)
# BigDecimal('1.2', 3, exception: true)
# BigDecimal('1.2', exception: true)
#
class BigDecimalWithNumericArgument < Base
extend AutoCorrector

MSG = 'Convert numeric argument to string before passing to `BigDecimal`.'
RESTRICT_ON_SEND = %i[BigDecimal].freeze
MSG = 'Convert numeric literal to string and pass it to `BigDecimal`.'
RESTRICT_ON_SEND = %i[BigDecimal to_d].freeze

def_node_matcher :big_decimal_with_numeric_argument?, <<~PATTERN
(send nil? :BigDecimal $numeric_type? ...)
PATTERN

def_node_matcher :to_d?, <<~PATTERN
(send [!nil? $numeric_type?] :to_d ...)
PATTERN

def on_send(node)
return unless (numeric = big_decimal_with_numeric_argument?(node))
return if numeric.float_type? && specifies_precision?(node)
if (numeric = big_decimal_with_numeric_argument?(node))
on_send_big_decimal(node, numeric)
elsif (numeric_to_d = to_d?(node))
on_send_to_d(node, numeric_to_d)
end
end

private

def on_send_big_decimal(node, numeric)
return if numeric.float_type? && specifies_precision?(node, position: 1)

add_offense(numeric.source_range) do |corrector|
corrector.wrap(numeric, "'", "'")
end
end

private
def on_send_to_d(node, numeric_to_d)
return if numeric_to_d.float_type? && specifies_precision?(node, position: 0)

add_offense(numeric_to_d.source_range) do |corrector|
big_decimal_args = node
.arguments
.map(&:source)
.unshift("'#{numeric_to_d.source}'")
.join(', ')

corrector.replace(node, "BigDecimal(#{big_decimal_args})")
end
end

def specifies_precision?(node)
node.arguments.size > 1 && !node.arguments[1].hash_type?
def specifies_precision?(node, position:)
node.arguments.size > position && !node.arguments[position].hash_type?
end
end
end
Expand Down
Expand Up @@ -4,7 +4,18 @@
it 'registers an offense and corrects when using `BigDecimal` with integer' do
expect_offense(<<~RUBY)
BigDecimal(1)
^ Convert numeric argument to string before passing to `BigDecimal`.
^ Convert numeric literal to string and pass it to `BigDecimal`.
RUBY

expect_correction(<<~RUBY)
BigDecimal('1')
RUBY
end

it 'registers an offense and corrects when using `Integer#to_d`' do
expect_offense(<<~RUBY)
1.to_d
^ Convert numeric literal to string and pass it to `BigDecimal`.
RUBY

expect_correction(<<~RUBY)
Expand All @@ -15,7 +26,18 @@
it 'registers an offense and corrects when using `BigDecimal` with float' do
expect_offense(<<~RUBY)
BigDecimal(1.5, exception: true)
^^^ Convert numeric argument to string before passing to `BigDecimal`.
^^^ Convert numeric literal to string and pass it to `BigDecimal`.
RUBY

expect_correction(<<~RUBY)
BigDecimal('1.5', exception: true)
RUBY
end

it 'registers an offense and corrects when using `Float#to_d`' do
expect_offense(<<~RUBY)
1.5.to_d(exception: true)
^^^ Convert numeric literal to string and pass it to `BigDecimal`.
RUBY

expect_correction(<<~RUBY)
Expand All @@ -29,22 +51,47 @@
RUBY
end

it 'does not register an offense when using `Float#to_d` with precision' do
expect_no_offenses(<<~RUBY)
3.14.to_d(1)
RUBY
end

it 'does not register an offense when using `BigDecimal` with float and non-literal precision' do
expect_no_offenses(<<~RUBY)
precision = 1
BigDecimal(3.14, precision)
RUBY
end

it 'does not register an offense when using `Float#to_d` with non-literal precision' do
expect_no_offenses(<<~RUBY)
precision = 1
3.14.to_d(precision)
RUBY
end

it 'does not register an offense when using `BigDecimal` with float, precision, and a keyword argument' do
expect_no_offenses(<<~RUBY)
BigDecimal(3.14, 1, exception: true)
RUBY
end

it 'does not register an offense when using `Float#to_d` with precision and a keyword argument' do
expect_no_offenses(<<~RUBY)
3.14.to_d(1, exception: true)
RUBY
end

it 'does not register an offense when using `BigDecimal` with string' do
expect_no_offenses(<<~RUBY)
BigDecimal('1')
RUBY
end

it 'does not register an offense when using `String#to_d`' do
expect_no_offenses(<<~RUBY)
'1'.to_d
RUBY
end
end

0 comments on commit b13c0ca

Please sign in to comment.