Skip to content

Commit 9bb461b

Browse files
committed
Fix false positives for try and try! in Rails/StrongParametersExpect
Fixes false positives in `Rails/StrongParametersExpect` for usages like `params[:key].try(:method)` and `params[:key].try!(:method)`.
1 parent 9585950 commit 9bb461b

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+
* [#1627](https://github.com/rubocop/rubocop-rails/pull/1627): Fix false positives in `Rails/StrongParametersExpect` for usages like `params[:key].try(:method)` and `params[:key].try!(:method)`. ([@nicholasdower][])

lib/rubocop/cop/rails/strong_parameters_expect.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class StrongParametersExpect < Base
5555
MSG = 'Use `%<prefer>s` instead.'
5656
RESTRICT_ON_SEND = %i[[] require permit].freeze
5757
PRESENCE_CHECK_METHODS = %i[nil? blank? present? presence].freeze
58-
NIL_SAFE_METHODS = %i[instance_of? is_a? kind_of? to_a to_f to_h to_i to_s].freeze
58+
NIL_SAFE_METHODS = %i[instance_of? is_a? kind_of? to_a to_f to_h to_i to_s try try!].freeze
5959
KEY_CHECK_METHODS = %i[key? has_key? include? member?].freeze
6060
RAISING_FINDER_METHODS = %i[find find_by! find_sole_by].freeze
6161

spec/rubocop/cop/rails/strong_parameters_expect_spec.rb

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

76+
it 'does not register an offense when using `params[:key].try(:method)`' do
77+
expect_no_offenses(<<~RUBY)
78+
params[:key].try(:method)
79+
RUBY
80+
end
81+
82+
it 'does not register an offense when using `params[:key].try!(:method)`' do
83+
expect_no_offenses(<<~RUBY)
84+
params[:key].try!(:method)
85+
RUBY
86+
end
87+
7688
it 'does not register an offense when using `params[:key].key?(:inner)`' do
7789
expect_no_offenses(<<~RUBY)
7890
params[:key].key?(:inner)

0 commit comments

Comments
 (0)