Skip to content

Commit 3e9d4a5

Browse files
authored
Merge pull request #1631 from koic/fix_false_positive_for_rails_strong_parameters_expect_with_negation
[Fix #1630] Fix a false positive in `Rails/StrongParametersExpect`
2 parents dbb2e27 + 3529b8d commit 3e9d4a5

3 files changed

Lines changed: 14 additions & 1 deletion

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#1630](https://github.com/rubocop/rubocop-rails/issues/1630): Fix a false positive in `Rails/StrongParametersExpect` when negating `params[:key]` with `!`, such as `!params[:key]`. ([@koic][])

lib/rubocop/cop/rails/strong_parameters_expect.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class StrongParametersExpect < Base
6060
# Covers presence/nil checks, nil-safe conversions and type checks, key-check methods,
6161
# and collection methods that imply `params[:key]` is a Hash/Array.
6262
IGNORED_METHODS = %i[
63-
blank? compact compact! compact_blank compact_blank! deep_merge deep_merge!
63+
! blank? compact compact! compact_blank compact_blank! deep_merge deep_merge!
6464
delete delete_if dig each except exclude? extract! fetch has_key? has_value?
6565
include? instance_of? is_a? keep_if key? keys kind_of? member? merge merge!
6666
nil? presence present? reverse_merge reverse_merge! slice stringify_keys

spec/rubocop/cop/rails/strong_parameters_expect_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,18 @@
207207
RUBY
208208
end
209209

210+
it 'does not register an offense when using `!params[:key]`' do
211+
expect_no_offenses(<<~RUBY)
212+
!params[:key]
213+
RUBY
214+
end
215+
216+
it 'does not register an offense when using `condition && !params[:key]`' do
217+
expect_no_offenses(<<~RUBY)
218+
condition && !params[:key]
219+
RUBY
220+
end
221+
210222
it 'does not register an offense when using `params[:key].blank?`' do
211223
expect_no_offenses(<<~RUBY)
212224
params[:key].blank?

0 commit comments

Comments
 (0)