Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Lint/RedundantSafeNavigation when using to_s #11915

Merged

Conversation

lucthev
Copy link
Contributor

@lucthev lucthev commented Jun 2, 2023

A Rails project I work on has code vaguely similar

maybe_time&.to_s(:iso8601) || '(Unknown)'

The changes made in #11890 cause Lint/RedundantSafeNavigation to suggest removing the safe navigation from the above code. This has been discussed before in #8867, but to summarize, removing the safe navigation is undesirable because:

  • nil.to_s(:iso8601) would raise an ArgumentError

  • Even in the more general case, when there are no extra arguments to to_s, removing the safe navigation changes the semantics:

    nil&.to_s || '(Unknown)' # => '(Unknown)'
    nil.to_s || '(Unknown)' # => ''

This PR reverts #11890 and adds a test to prevent regressions.


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.

@koic
Copy link
Member

koic commented Jun 2, 2023

Oops! I made the mistake. Thank you for catching up!

@koic koic merged commit 1181d4e into rubocop:master Jun 2, 2023
28 checks passed
@lucthev lucthev deleted the redundant_safe_navigation_false_positive branch June 2, 2023 18:06
mkllnk added a commit to openfoodfoundation/openfoodnetwork that referenced this pull request Jun 5, 2023
The cop suggested to change more but they were false positives:

* rubocop/rubocop#11915
@john-h-k
Copy link

john-h-k commented Jun 6, 2023

Could we get a minor release for this?

@bbatsov
Copy link
Collaborator

bbatsov commented Jun 6, 2023

@john-h-k I'll likely cut one before the end of the week. There are a few other bug-fix PRs I'd like to merge first.

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.

None yet

4 participants