Skip to content

Commit 1346310

Browse files
authored
Merge pull request #1624 from koic/fix_false_positives_for_rails_strong_parameters_expect_with_key_check_methods
[Fix #1622] Fix false positives in `Rails/StrongParametersExpect`
2 parents 9524ba1 + 64422a9 commit 1346310

3 files changed

Lines changed: 31 additions & 3 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#1622](https://github.com/rubocop/rubocop-rails/issues/1622): Fix false positives in `Rails/StrongParametersExpect` when using key-check methods such as `key?`, `has_key?`, `include?`, and `member?` on `params[:key]`. ([@koic][])

lib/rubocop/cop/rails/strong_parameters_expect.rb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ module Rails
88
# In the following cases, `params[:key]` is treated as a key that is expected to be passed from the HTTP client,
99
# and the cop detects it using the `expect` method.
1010
#
11-
# - Method calls on `params[:key]` without comparison methods or methods that are safe to call
12-
# on `nil` (such as `to_i`, `to_s`, or `is_a?`)
11+
# - Method calls on `params[:key]` without comparison methods, methods that are safe to call
12+
# on `nil` (such as `to_i`, `to_s`, or `is_a?`), or key-check methods such as `key?`
1313
# - Passing `params[:key]` as an argument to finder methods that raise on missing records
1414
# - Strong parameter methods using `require` or `permit`
1515
#
@@ -56,6 +56,7 @@ class StrongParametersExpect < Base
5656
RESTRICT_ON_SEND = %i[[] require permit].freeze
5757
PRESENCE_CHECK_METHODS = %i[nil? blank? present? presence].freeze
5858
NIL_SAFE_METHODS = %i[instance_of? is_a? kind_of? to_a to_f to_h to_i to_s].freeze
59+
KEY_CHECK_METHODS = %i[key? has_key? include? member?].freeze
5960
RAISING_FINDER_METHODS = %i[find find_by! find_sole_by].freeze
6061

6162
minimum_target_rails_version 8.0
@@ -137,7 +138,9 @@ def offensive_bracket_access?(node)
137138

138139
method_name = parent.method_name
139140

140-
!PRESENCE_CHECK_METHODS.include?(method_name) && !NIL_SAFE_METHODS.include?(method_name)
141+
!PRESENCE_CHECK_METHODS.include?(method_name) &&
142+
!NIL_SAFE_METHODS.include?(method_name) &&
143+
!KEY_CHECK_METHODS.include?(method_name)
141144
else
142145
raising_finder_method?(parent)
143146
end

spec/rubocop/cop/rails/strong_parameters_expect_spec.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,30 @@
7373
RUBY
7474
end
7575

76+
it 'does not register an offense when using `params[:key].key?(:inner)`' do
77+
expect_no_offenses(<<~RUBY)
78+
params[:key].key?(:inner)
79+
RUBY
80+
end
81+
82+
it 'does not register an offense when using `params[:key].has_key?(:inner)`' do
83+
expect_no_offenses(<<~RUBY)
84+
params[:key].has_key?(:inner)
85+
RUBY
86+
end
87+
88+
it 'does not register an offense when using `params[:key].include?(:inner)`' do
89+
expect_no_offenses(<<~RUBY)
90+
params[:key].include?(:inner)
91+
RUBY
92+
end
93+
94+
it 'does not register an offense when using `params[:key].member?(:inner)`' do
95+
expect_no_offenses(<<~RUBY)
96+
params[:key].member?(:inner)
97+
RUBY
98+
end
99+
76100
it "does not register an offense when using `params[:key] == 'value'`" do
77101
expect_no_offenses(<<~RUBY)
78102
params[:key] == 'value'

0 commit comments

Comments
 (0)