Skip to content

Commit

Permalink
Merge pull request #246 from koic/mark_delete_prefix_and_delete_suffi…
Browse files Browse the repository at this point in the history
…x_as_unsafe

[Fix #245] Mark `Performance/DeletePrefix` and `Performance/DeleteSuffix` as unsafe
  • Loading branch information
koic committed May 16, 2021
2 parents e52ebcc + 213c4b8 commit 1fff69f
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## master (unreleased)

### Changes

* [#245](https://github.com/rubocop/rubocop-performance/issues/245): Mark `Performance/DeletePrefix` and `Performance/DeleteSuffix` as unsafe. ([@koic][])

## 1.11.3 (2021-05-06)

### Bug fixes
Expand Down
4 changes: 4 additions & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,18 @@ Performance/Count:
Performance/DeletePrefix:
Description: 'Use `delete_prefix` instead of `gsub`.'
Enabled: true
Safe: false
SafeMultiline: true
VersionAdded: '1.6'
VersionChanged: '1.12'

Performance/DeleteSuffix:
Description: 'Use `delete_suffix` instead of `gsub`.'
Enabled: true
Safe: false
SafeMultiline: true
VersionAdded: '1.6'
VersionChanged: '1.12'

Performance/Detect:
Description: >-
Expand Down
14 changes: 8 additions & 6 deletions docs/modules/ROOT/pages/cops_performance.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -548,16 +548,17 @@ NOTE: Required Ruby version: 2.5
| Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged

| Enabled
| Yes
| Yes
| No
| Yes (Unsafe)
| 1.6
| -
| 1.12
|===

In Ruby 2.5, `String#delete_prefix` has been added.

This cop identifies places where `gsub(/\Aprefix/, '')` and `sub(/\Aprefix/, '')`
can be replaced by `delete_prefix('prefix')`.
It is marked as unsafe by default because `Pathname` has `sub` but not `delete_prefix`.

This cop has `SafeMultiline` configuration option that `true` by default because
`^prefix` is unsafe as it will behave incompatible with `delete_prefix`
Expand Down Expand Up @@ -621,16 +622,17 @@ NOTE: Required Ruby version: 2.5
| Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged

| Enabled
| Yes
| Yes
| No
| Yes (Unsafe)
| 1.6
| -
| 1.12
|===

In Ruby 2.5, `String#delete_suffix` has been added.

This cop identifies places where `gsub(/suffix\z/, '')` and `sub(/suffix\z/, '')`
can be replaced by `delete_suffix('suffix')`.
It is marked as unsafe by default because `Pathname` has `sub` but not `delete_suffix`.

This cop has `SafeMultiline` configuration option that `true` by default because
`suffix$` is unsafe as it will behave incompatible with `delete_suffix?`
Expand Down
1 change: 1 addition & 0 deletions lib/rubocop/cop/performance/delete_prefix.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module Performance
#
# This cop identifies places where `gsub(/\Aprefix/, '')` and `sub(/\Aprefix/, '')`
# can be replaced by `delete_prefix('prefix')`.
# It is marked as unsafe by default because `Pathname` has `sub` but not `delete_prefix`.
#
# This cop has `SafeMultiline` configuration option that `true` by default because
# `^prefix` is unsafe as it will behave incompatible with `delete_prefix`
Expand Down
1 change: 1 addition & 0 deletions lib/rubocop/cop/performance/delete_suffix.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module Performance
#
# This cop identifies places where `gsub(/suffix\z/, '')` and `sub(/suffix\z/, '')`
# can be replaced by `delete_suffix('suffix')`.
# It is marked as unsafe by default because `Pathname` has `sub` but not `delete_suffix`.
#
# This cop has `SafeMultiline` configuration option that `true` by default because
# `suffix$` is unsafe as it will behave incompatible with `delete_suffix?`
Expand Down

0 comments on commit 1fff69f

Please sign in to comment.