Skip to content

Commit

Permalink
Merge pull request #150 from eugeneius/bigdecimal_float_precision
Browse files Browse the repository at this point in the history
Don't autocorrect BigDecimal with float when precision is specified
  • Loading branch information
koic committed Jul 11, 2020
2 parents 337d869 + 80bbdab commit 3c83bf5
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Bug fixes

* [#147](https://github.com/rubocop-hq/rubocop-performance/issues/147): Fix an error for `Performance/AncestorsInclude` when using `ancestors.include?` without receiver. ([@koic][])
* [#150](https://github.com/rubocop-hq/rubocop-performance/pull/150): Fix an incorrect autocorrect for `Performance/BigDecimalWithNumericArgument` when a precision is specified. ([@eugeneius][])

### Changes

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ class BigDecimalWithNumericArgument < Cop

def on_send(node)
big_decimal_with_numeric_argument?(node) do |numeric|
next if numeric.float_type? && specifies_precision?(node)

add_offense(node, location: numeric.source_range)
end
end
Expand All @@ -36,6 +38,12 @@ def autocorrect(node)
end
end
end

private

def specifies_precision?(node)
node.arguments.size > 1 && !node.arguments[1].hash_type?
end
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,31 @@

it 'registers an offense and corrects when using `BigDecimal` with float' do
expect_offense(<<~RUBY)
BigDecimal(1.5, 2, exception: true)
BigDecimal(1.5, exception: true)
^^^ Convert numeric argument to string before passing to `BigDecimal`.
RUBY

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

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

Expand Down

0 comments on commit 3c83bf5

Please sign in to comment.