Skip to content

Fix false positives in Rails/StrongParametersExpect#1616

Merged
koic merged 1 commit into
rubocop:masterfrom
koic:fix_false_positives_in_rails_strong_parameters_expect
May 13, 2026
Merged

Fix false positives in Rails/StrongParametersExpect#1616
koic merged 1 commit into
rubocop:masterfrom
koic:fix_false_positives_in_rails_strong_parameters_expect

Conversation

@koic
Copy link
Copy Markdown
Member

@koic koic commented May 11, 2026

This PR fixes false positives in Rails/StrongParametersExpect when params[:key] is followed by a nil-safe conversion method such as to_i, to_s, to_a, to_f, or to_h.

These methods are defined on NilClass and return a sensible default (e.g., nil.to_i returns 0) without raising, so the caller is intentionally treating the parameter as optional. This is the same rationale behind the existing PRESENCE_CHECK_METHODS allow list (nil?, blank?, present?, presence).


Before submitting the PR make sure the following are checked:

  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Ran bundle exec rake default. It executes all tests and runs RuboCop on its own code.
  • Added an entry (file) to the changelog folder named {change_type}_{change_description}.md if the new code introduces user-observable changes. See changelog entry format for details.
  • If this is a new cop, consider making a corresponding update to the Rails Style Guide.

This PR fixes false positives in `Rails/StrongParametersExpect`
when `params[:key]` is followed by a nil-safe conversion method such as
`to_i`, `to_s`, `to_a`, `to_f`, or `to_h`.

These methods are defined on `NilClass` and return a sensible default
(e.g., `nil.to_i` returns `0`) without raising, so the caller is intentionally treating
the parameter as optional. This is the same rationale behind the existing `PRESENCE_CHECK_METHODS`
allow list (`nil?`, `blank?`, `present?`, `presence`).
@koic koic merged commit e18a76b into rubocop:master May 13, 2026
17 checks passed
@koic koic deleted the fix_false_positives_in_rails_strong_parameters_expect branch May 13, 2026 16:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant