Skip to content

[Fix #1630] Fix a false positive in Rails/StrongParametersExpect#1631

Merged
koic merged 1 commit into
rubocop:masterfrom
koic:fix_false_positive_for_rails_strong_parameters_expect_with_negation
May 26, 2026
Merged

[Fix #1630] Fix a false positive in Rails/StrongParametersExpect#1631
koic merged 1 commit into
rubocop:masterfrom
koic:fix_false_positive_for_rails_strong_parameters_expect_with_negation

Conversation

@koic
Copy link
Copy Markdown
Member

@koic koic commented May 26, 2026

This PR fixes a false positive in Rails/StrongParametersExpect when negating params[:key] with !, such as !params[:key] or condition && !params[:key].

Negating params[:key] is a way of checking for an optional parameter, where a missing value is expected and should be falsy. Rewriting !params[:key] to !params.expect(:key) changes the runtime behavior, because params.expect(:key) raises ActionController::ParameterMissing when the parameter is absent instead of returning a falsy value.

Fixes #1630.


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.

…ect`

This PR fixes a false positive in `Rails/StrongParametersExpect` when negating
`params[:key]` with `!`, such as `!params[:key]` or `condition && !params[:key]`.

Negating `params[:key]` is a way of checking for an optional parameter,
where a missing value is expected and should be falsy.
Rewriting `!params[:key]` to `!params.expect(:key)` changes the runtime behavior,
because `params.expect(:key)` raises `ActionController::ParameterMissing`
when the parameter is absent instead of returning a falsy value.

Fixes rubocop#1630.
@koic koic merged commit 3e9d4a5 into rubocop:master May 26, 2026
17 checks passed
@koic koic deleted the fix_false_positive_for_rails_strong_parameters_expect_with_negation branch May 26, 2026 18:48
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.

False positive with Rails/StrongParametersExpect and optional parameter check

1 participant