Skip to content

Commit 7dabfc7

Browse files
committed
Mark Rails/CompactBlank as unsafe
Follow up to #598. This PR marks `Rails/CompactBlank` as unsafe. It is unsafe by default because false positives may occur in the blank check of block arguments to the receiver object. ```ruby [[1, 2], [3, nil]].reject { |first, second| second.blank? } # => [[1, 2]] [[1, 2], [3, nil]].compact_blank # => [[1, 2], [3, nil]] ```
1 parent a80ac3b commit 7dabfc7

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

config/default.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ Rails/BulkChangeTable:
183183
Rails/CompactBlank:
184184
Description: 'Checks if collection can be blank-compacted with `compact_blank`.'
185185
Enabled: pending
186+
Safe: false
186187
VersionAdded: '<<next>>'
187188

188189
Rails/ContentTag:

lib/rubocop/cop/rails/compact_blank.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@ module Cop
55
module Rails
66
# Checks if collection can be blank-compacted with `compact_blank`.
77
#
8+
# @safety
9+
# It is unsafe by default because false positives may occur in the
10+
# blank check of block arguments to the receiver object.
11+
#
12+
# For example, `[[1, 2], [3, nil]].reject { |first, second| second.blank? }` and
13+
# `[[1, 2], [3, nil]].compact_blank` are not compatible. The same is true for `empty?`.
14+
# This will work fine when the receiver is a hash object.
15+
#
816
# @example
917
#
1018
# # bad

0 commit comments

Comments
 (0)