diff --git a/CHANGELOG.md b/CHANGELOG.md index e7f995fd71..977f3ec2e4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/config/default.yml b/config/default.yml index e617662bd4..108600a40d 100644 --- a/config/default.yml +++ b/config/default.yml @@ -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: >- diff --git a/docs/modules/ROOT/pages/cops_performance.adoc b/docs/modules/ROOT/pages/cops_performance.adoc index 1e3212ba08..b6395e8003 100644 --- a/docs/modules/ROOT/pages/cops_performance.adoc +++ b/docs/modules/ROOT/pages/cops_performance.adoc @@ -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` @@ -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?` diff --git a/lib/rubocop/cop/performance/delete_prefix.rb b/lib/rubocop/cop/performance/delete_prefix.rb index f023ffbc20..5d76629ffe 100644 --- a/lib/rubocop/cop/performance/delete_prefix.rb +++ b/lib/rubocop/cop/performance/delete_prefix.rb @@ -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` diff --git a/lib/rubocop/cop/performance/delete_suffix.rb b/lib/rubocop/cop/performance/delete_suffix.rb index d4251210bd..8deb2d5ae7 100644 --- a/lib/rubocop/cop/performance/delete_suffix.rb +++ b/lib/rubocop/cop/performance/delete_suffix.rb @@ -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?`