@@ -13,6 +13,12 @@ module Rails
1313 # `[[1, 2], [3, nil]].compact_blank` are not compatible. The same is true for `blank?`.
1414 # This will work fine when the receiver is a hash object.
1515 #
16+ # And `compact_blank!` has different implementations for `Array`, `Hash`, and
17+ # `ActionController::Parameters`.
18+ # `Array#compact_blank!`, `Hash#compact_blank!` are equivalent to `delete_if(&:blank?)`.
19+ # `ActionController::Parameters#compact_blank!` is equivalent to `reject!(&:blank?)`.
20+ # If the cop makes a mistake, auto-corrected code may get unexpected behavior.
21+ #
1622 # @example
1723 #
1824 # # bad
@@ -23,8 +29,10 @@ module Rails
2329 # collection.compact_blank
2430 #
2531 # # bad
26- # collection.reject!(&:blank?)
27- # collection.reject! { |_k, v| v.blank? }
32+ # collection.delete_if(&:blank?) # Same behavior as `Array#compact_blank!` and `Hash#compact_blank!`
33+ # collection.delete_if { |_k, v| v.blank? } # Same behavior as `Array#compact_blank!` and `Hash#compact_blank!`
34+ # collection.reject!(&:blank?) # Same behavior as `ActionController::Parameters#compact_blank!`
35+ # collection.reject! { |_k, v| v.blank? } # Same behavior as `ActionController::Parameters#compact_blank!`
2836 #
2937 # # good
3038 # collection.compact_blank!
@@ -35,20 +43,20 @@ class CompactBlank < Base
3543 extend TargetRailsVersion
3644
3745 MSG = 'Use `%<preferred_method>s` instead.'
38- RESTRICT_ON_SEND = %i[ reject reject! ] . freeze
46+ RESTRICT_ON_SEND = %i[ reject delete_if reject! ] . freeze
3947
4048 minimum_target_rails_version 6.1
4149
4250 def_node_matcher :reject_with_block? , <<~PATTERN
4351 (block
44- (send _ {:reject :reject!})
52+ (send _ {:reject :delete_if : reject!})
4553 $(args ...)
4654 (send
4755 $(lvar _) :blank?))
4856 PATTERN
4957
5058 def_node_matcher :reject_with_block_pass? , <<~PATTERN
51- (send _ {:reject :reject!}
59+ (send _ {:reject :delete_if : reject!}
5260 (block_pass
5361 (sym :blank?)))
5462 PATTERN
0 commit comments