diff --git a/CHANGELOG.md b/CHANGELOG.md index 977f3ec2e4..f3cfaa7023 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ ### Changes * [#245](https://github.com/rubocop/rubocop-performance/issues/245): Mark `Performance/DeletePrefix` and `Performance/DeleteSuffix` as unsafe. ([@koic][]) +* [#249](https://github.com/rubocop/rubocop-performance/issues/249): Mark `Performance/RedundantStringChars` as unsafe. ([@tejasbubane][]) ## 1.11.3 (2021-05-06) diff --git a/config/default.yml b/config/default.yml index 108600a40d..4f6caf2a44 100644 --- a/config/default.yml +++ b/config/default.yml @@ -248,8 +248,10 @@ Performance/RedundantSplitRegexpArgument: Performance/RedundantStringChars: Description: 'Checks for redundant `String#chars`.' + Safe: false Enabled: 'pending' VersionAdded: '1.7' + VersionChanged: '1.12' Performance/RegexpMatch: Description: >- diff --git a/docs/modules/ROOT/pages/cops_performance.adoc b/docs/modules/ROOT/pages/cops_performance.adoc index b6395e8003..79017c755c 100644 --- a/docs/modules/ROOT/pages/cops_performance.adoc +++ b/docs/modules/ROOT/pages/cops_performance.adoc @@ -1425,10 +1425,10 @@ a deterministic regexp to a string. | Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged | Pending -| Yes -| Yes +| No +| Yes (Unsafe) | 1.7 -| - +| 1.12 |=== This cop checks for redundant `String#chars`. diff --git a/lib/rubocop/cop/performance/redundant_string_chars.rb b/lib/rubocop/cop/performance/redundant_string_chars.rb index 9d5eb625c6..a85bc5bdc0 100644 --- a/lib/rubocop/cop/performance/redundant_string_chars.rb +++ b/lib/rubocop/cop/performance/redundant_string_chars.rb @@ -4,6 +4,9 @@ module RuboCop module Cop module Performance # This cop checks for redundant `String#chars`. + # It is marked unsafe by default because suggested autocorrections like + # `str[-2..-1]` could return `nil` when `str` holds short values and + # `.chars` on that can raise error - resulting in unsafe behaviour. # # @example # # bad