From 41215153a446ac1309414feb63ebac7c7ea95c29 Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Sat, 6 Mar 2021 05:01:14 +0900 Subject: [PATCH] [Fix #444] Mark `Rails/Blank` as unsafe auto-correction Fixes ##444. This PR marks `Rails/Blank` as unsafe auto-correction because `' '.empty?` returns false, but `' '.blank?` returns true. Therefore, auto-correction is not compatible if receiver is a non-empty blank string, tab, or newline meta characters. --- CHANGELOG.md | 1 + config/default.yml | 3 ++- docs/modules/ROOT/pages/cops_rails.adoc | 8 ++++++-- lib/rubocop/cop/rails/blank.rb | 4 ++++ 4 files changed, 13 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e77a8956d4..06860a3cda 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ * [#432](https://github.com/rubocop/rubocop-rails/issues/432): Exclude gemspec file by default for `Rails/TimeZone` cop. ([@koic][]) * [#440](https://github.com/rubocop/rubocop-rails/issues/440): This PR makes `Rails/TimeZone` aware of timezone specifier. ([@koic][]) * [#381](https://github.com/rubocop/rubocop-rails/pull/381): Update `IgnoredMethods` list for `Lint/NumberConversion` to allow Rails' duration methods. ([@dvandersluis][]) +* [#444](https://github.com/rubocop/rubocop-rails/issues/444): Mark `Rails/Blank` as unsafe auto-correction. ([@koic][]) ## 2.9.1 (2020-12-16) diff --git a/config/default.yml b/config/default.yml index a8311ac9dd..261c816184 100644 --- a/config/default.yml +++ b/config/default.yml @@ -149,8 +149,9 @@ Rails/BelongsTo: Rails/Blank: Description: 'Enforces use of `blank?`.' Enabled: true + SafeAutoCorrect: false VersionAdded: '0.48' - VersionChanged: '0.67' + VersionChanged: '2.10' # Convert usages of `nil? || empty?` to `blank?` NilOrEmpty: true # Convert usages of `!present?` to `blank?` diff --git a/docs/modules/ROOT/pages/cops_rails.adoc b/docs/modules/ROOT/pages/cops_rails.adoc index eb567073b3..e1d839d092 100644 --- a/docs/modules/ROOT/pages/cops_rails.adoc +++ b/docs/modules/ROOT/pages/cops_rails.adoc @@ -590,14 +590,18 @@ end | Enabled | Yes -| Yes +| Yes (Unsafe) | 0.48 -| 0.67 +| 2.10 |=== This cop checks for code that can be written with simpler conditionals using `Object#blank?` defined by Active Support. +This cop is marked as unsafe auto-correction, because `' '.empty?` returns false, +but `' '.blank?` returns true. Therefore, auto-correction is not compatible +if receiver is a non-empty blank string, tab, or newline meta characters. + Interaction with `Style/UnlessElse`: The configuration of `NotPresent` will not produce an offense in the context of `unless else` if `Style/UnlessElse` is inabled. This is diff --git a/lib/rubocop/cop/rails/blank.rb b/lib/rubocop/cop/rails/blank.rb index 8b530abb6d..2e610a9f86 100644 --- a/lib/rubocop/cop/rails/blank.rb +++ b/lib/rubocop/cop/rails/blank.rb @@ -6,6 +6,10 @@ module Rails # This cop checks for code that can be written with simpler conditionals # using `Object#blank?` defined by Active Support. # + # This cop is marked as unsafe auto-correction, because `' '.empty?` returns false, + # but `' '.blank?` returns true. Therefore, auto-correction is not compatible + # if receiver is a non-empty blank string, tab, or newline meta characters. + # # Interaction with `Style/UnlessElse`: # The configuration of `NotPresent` will not produce an offense in the # context of `unless else` if `Style/UnlessElse` is inabled. This is