From 32fdcafb1a1ce8ab95a74d6049d0e167541b0c88 Mon Sep 17 00:00:00 2001 From: Eugene Kenny Date: Sun, 20 Sep 2020 19:44:55 +0100 Subject: [PATCH] Ignore non-integer ranges in Style/RandomWithOffset This cop only works correctly for ranges with integer bounds. --- CHANGELOG.md | 1 + lib/rubocop/cop/style/random_with_offset.rb | 6 ++--- .../cop/style/random_with_offset_spec.rb | 24 +++++++++++++++++++ 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a481ed9e3d7..75c67127dc5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ * [#8739](https://github.com/rubocop-hq/rubocop/issues/8739): Fix an error for `Lint/UselessTimes` when using empty block argument. ([@koic][]) * [#8742](https://github.com/rubocop-hq/rubocop/issues/8742): Fix some assignment counts for `Metrics/AbcSize`. ([@marcandre][]) * [#8750](https://github.com/rubocop-hq/rubocop/pull/8750): Fix an incorrect auto-correct for `Style/MultilineWhenThen` when line break for multiple condidate values of `when` statement. ([@koic][]) +* [#8754](https://github.com/rubocop-hq/rubocop/pull/8754): Fix an error for `Style/RandomWithOffset` when using a range with non-integer bounds. ([@eugeneius][]) ### Changes diff --git a/lib/rubocop/cop/style/random_with_offset.rb b/lib/rubocop/cop/style/random_with_offset.rb index 4da28c90e52..644269a7181 100644 --- a/lib/rubocop/cop/style/random_with_offset.rb +++ b/lib/rubocop/cop/style/random_with_offset.rb @@ -36,7 +36,7 @@ class RandomWithOffset < Base (send {nil? (const {nil? cbase} :Random) (const {nil? cbase} :Kernel)} :rand - {int irange erange})) + {int (irange int int) (erange int int)})) PATTERN def_node_matcher :rand_op_integer?, <<~PATTERN @@ -44,7 +44,7 @@ class RandomWithOffset < Base (send {nil? (const {nil? cbase} :Random) (const {nil? cbase} :Kernel)} :rand - {int irange erange}) + {int (irange int int) (erange int int)}) {:+ :-} int) PATTERN @@ -54,7 +54,7 @@ class RandomWithOffset < Base (send {nil? (const {nil? cbase} :Random) (const {nil? cbase} :Kernel)} :rand - {int irange erange}) + {int (irange int int) (erange int int)}) {:succ :pred :next}) PATTERN diff --git a/spec/rubocop/cop/style/random_with_offset_spec.rb b/spec/rubocop/cop/style/random_with_offset_spec.rb index e7bc14a1693..64530575735 100644 --- a/spec/rubocop/cop/style/random_with_offset_spec.rb +++ b/spec/rubocop/cop/style/random_with_offset_spec.rb @@ -258,6 +258,30 @@ RUBY end + it 'does not register an offense when using rand(irange) + offset with a non-integer range value' do + expect_no_offenses(<<~RUBY) + rand(0..limit) + 1 + RUBY + end + + it 'does not register an offense when using offset - rand(erange) with a non-integer range value' do + expect_no_offenses(<<~RUBY) + 1 - rand(0...limit) + RUBY + end + + it 'does not register an offense when using rand(irange).succ with a non-integer range value' do + expect_no_offenses(<<~RUBY) + rand(0..limit).succ + RUBY + end + + it 'does not register an offense when using rand(erange).pred with a non-integer range value' do + expect_no_offenses(<<~RUBY) + rand(0...limit).pred + RUBY + end + it 'does not register an offense when using range with double dots' do expect_no_offenses('rand(1..6)') end