Skip to content

Commit e18a76b

Browse files
authored
Merge pull request #1616 from koic/fix_false_positives_in_rails_strong_parameters_expect
Fix false positives in `Rails/StrongParametersExpect`
2 parents cf52bef + 3f317b7 commit e18a76b

3 files changed

Lines changed: 37 additions & 3 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#1616](https://github.com/rubocop/rubocop-rails/pull/1616): Fix false positives in `Rails/StrongParametersExpect` when using nil-safe conversion methods such as `to_i`, `to_s`, `to_a`, `to_f`, and `to_h` 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,7 +8,7 @@ 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
11+
# - Method calls on `params[:key]` without comparison methods and nil-safe conversion methods
1212
# - Passing `params[:key]` as an argument to finder methods that raise on missing records
1313
# - Strong parameter methods using `require` or `permit`
1414
#
@@ -54,6 +54,7 @@ class StrongParametersExpect < Base
5454
MSG = 'Use `%<prefer>s` instead.'
5555
RESTRICT_ON_SEND = %i[[] require permit].freeze
5656
PRESENCE_CHECK_METHODS = %i[nil? blank? present? presence].freeze
57+
NIL_SAFE_CONVERSION_METHODS = %i[to_a to_f to_h to_i to_s].freeze
5758
RAISING_FINDER_METHODS = %i[find find_by! find_sole_by].freeze
5859

5960
minimum_target_rails_version 8.0
@@ -131,9 +132,11 @@ def offensive_bracket_access?(node)
131132
return false unless parent.call_type?
132133

133134
if parent.receiver == node
134-
return false if parent.comparison_method?
135+
return false if parent.comparison_method? || parent.method?(:[])
135136

136-
!parent.method?(:[]) && !PRESENCE_CHECK_METHODS.include?(parent.method_name)
137+
method_name = parent.method_name
138+
139+
!PRESENCE_CHECK_METHODS.include?(method_name) && !NIL_SAFE_CONVERSION_METHODS.include?(method_name)
137140
else
138141
raising_finder_method?(parent)
139142
end

spec/rubocop/cop/rails/strong_parameters_expect_spec.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,36 @@
2525
RUBY
2626
end
2727

28+
it 'does not register an offense when using `key = params[:key].to_i`' do
29+
expect_no_offenses(<<~RUBY)
30+
params[:key].to_i
31+
RUBY
32+
end
33+
34+
it 'does not register an offense when using `params[:key].to_s`' do
35+
expect_no_offenses(<<~RUBY)
36+
params[:key].to_s
37+
RUBY
38+
end
39+
40+
it 'does not register an offense when using `params[:key].to_a`' do
41+
expect_no_offenses(<<~RUBY)
42+
params[:key].to_a
43+
RUBY
44+
end
45+
46+
it 'does not register an offense when using `params[:key].to_f`' do
47+
expect_no_offenses(<<~RUBY)
48+
params[:key].to_f
49+
RUBY
50+
end
51+
52+
it 'does not register an offense when using `params[:key].to_h`' do
53+
expect_no_offenses(<<~RUBY)
54+
params[:key].to_h
55+
RUBY
56+
end
57+
2858
it "does not register an offense when using `params[:key] == 'value'`" do
2959
expect_no_offenses(<<~RUBY)
3060
params[:key] == 'value'

0 commit comments

Comments
 (0)