Skip to content

[Fix #1620] Fix false positives in Rails/StrongParametersExpect#1623

Merged
koic merged 1 commit into
rubocop:masterfrom
koic:fix_false_positives_for_rails_strong_parameters_expect_with_type_check_methods
May 16, 2026
Merged

[Fix #1620] Fix false positives in Rails/StrongParametersExpect#1623
koic merged 1 commit into
rubocop:masterfrom
koic:fix_false_positives_for_rails_strong_parameters_expect_with_type_check_methods

Conversation

@koic
Copy link
Copy Markdown
Member

@koic koic commented May 15, 2026

This PR fixes false positives in Rails/StrongParametersExpect when params[:key] is followed by a type-check method such as is_a?, kind_of?, or instance_of?.

These methods are inherited by NilClass from BasicObject/Object, so calling them on a missing parameter simply returns false instead of raising. Callers using params[:key].is_a?(...) are intentionally treating the parameter as optional, and rewriting the expression to params.expect(:key).is_a?(...) changes that semantics because expect raises ActionController::ParameterMissing when the parameter is absent.

Since this shares the same "safe to call on nil" rationale as the existing NIL_SAFE_CONVERSION_METHODS (to_a, to_f, to_h, to_i, to_s), the constant is renamed to NIL_SAFE_METHODS and the type-check methods are merged into it rather than introducing a separate constant.


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 type-check method such as
`is_a?`, `kind_of?`, or `instance_of?`.

These methods are inherited by `NilClass` from `BasicObject`/`Object`,
so calling them on a missing parameter simply returns `false` instead
of raising. Callers using `params[:key].is_a?(...)` are intentionally
treating the parameter as optional, and rewriting the expression to
`params.expect(:key).is_a?(...)` changes that semantics because
`expect` raises `ActionController::ParameterMissing` when the
parameter is absent.

Since this shares the same "safe to call on `nil`" rationale as the
existing `NIL_SAFE_CONVERSION_METHODS` (`to_a`, `to_f`, `to_h`, `to_i`,
`to_s`), the constant is renamed to `NIL_SAFE_METHODS` and the
type-check methods are merged into it rather than introducing a
separate constant.
@koic koic merged commit 9524ba1 into rubocop:master May 16, 2026
17 checks passed
@koic koic deleted the fix_false_positives_for_rails_strong_parameters_expect_with_type_check_methods branch May 16, 2026 02:38
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