Skip to content

Commit

Permalink
[Fix rubocop#444] Mark Rails/Blank as unsafe auto-correction
Browse files Browse the repository at this point in the history
Fixes #rubocop#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.
  • Loading branch information
koic committed Mar 8, 2021
1 parent 84b0829 commit 4121515
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
3 changes: 2 additions & 1 deletion config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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?`
Expand Down
8 changes: 6 additions & 2 deletions docs/modules/ROOT/pages/cops_rails.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions lib/rubocop/cop/rails/blank.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 4121515

Please sign in to comment.