Skip to content

Commit

Permalink
[Fix #6641] Specify Performance/RangeInclude as unsafe
Browse files Browse the repository at this point in the history
Fixes #6641.

This PR specifies `Performance/RangeInclude` as unsafe because
`Range#include?` and `Range#cover?` are not equivalent behaviour.
  • Loading branch information
koic authored and bbatsov committed Jan 11, 2019
1 parent 0b16720 commit 2177c2b
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -10,6 +10,10 @@
* [#6617](https://github.com/rubocop-hq/rubocop/issues/6617): Prevent traversal error on infinite ranges. ([@drenmi][])
* [#6625](https://github.com/rubocop-hq/rubocop/issues/6625): Revert the "auto-exclusion of files ignored by git" feature. ([@bbatsov][])

### Changes

* [#6641](https://github.com/rubocop-hq/rubocop/issues/6641): Specify `Performance/RangeInclude` as unsafe because `Range#include?` and `Range#cover?` are not equivalent. ([@koic][])

## 0.62.0 (2019-01-01)

### New features
Expand Down
1 change: 1 addition & 0 deletions config/default.yml
Expand Up @@ -2074,6 +2074,7 @@ Performance/RangeInclude:
Reference: 'https://github.com/JuanitoFatas/fast-ruby#cover-vs-include-code'
Enabled: true
VersionAdded: '0.36'
Safe: false

Performance/RedundantBlockCall:
Description: 'Use `yield` instead of `block.call`.'
Expand Down
3 changes: 3 additions & 0 deletions lib/rubocop/cop/performance/range_include.rb
Expand Up @@ -9,6 +9,9 @@ module Performance
# end points of the `Range`. In a great majority of cases, this is what
# is wanted.
#
# This cop is `Safe: false` by default because `Range#include?` and
# `Range#cover?` are not equivalent behaviour.
#
# @example
# # bad
# ('a'..'z').include?('b') # => true
Expand Down
5 changes: 4 additions & 1 deletion manual/cops_performance.md
Expand Up @@ -515,14 +515,17 @@ end

Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged
--- | --- | --- | --- | ---
Enabled | Yes | Yes | 0.36 | -
Enabled | No | Yes | 0.36 | -

This cop identifies uses of `Range#include?`, which iterates over each
item in a `Range` to see if a specified item is there. In contrast,
`Range#cover?` simply compares the target item with the beginning and
end points of the `Range`. In a great majority of cases, this is what
is wanted.

This cop is `Safe: false` by default because `Range#include?` and
`Range#cover?` are not equivalent behaviour.

### Examples

```ruby
Expand Down

0 comments on commit 2177c2b

Please sign in to comment.