KEMBAR78
Make `Lint/RedundantSafeNavigation` aware of builtin methods by koic · Pull Request #14375 · rubocop/rubocop · GitHub
Skip to content

Conversation

@koic
Copy link
Member

@koic koic commented Jul 20, 2025

This PR makes Lint/RedundantSafeNavigation aware of builtin convert methods to_s, to_i, to_f, to_a, and to_h.

# bad
foo.to_s&.strip
foo.to_i&.zero?
foo.to_f&.zero?
foo.to_a&.size
foo.to_h&.size

# good
foo.to_s.strip
foo.to_i.zero?
foo.to_f.zero?
foo.to_a.size
foo.to_h.size

These conversion methods don't return nil.


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.

This PR makes `Lint/RedundantSafeNavigation` aware of builtin convert methods
`to_s`, `to_i`, `to_f`, `to_a, and `to_h`.

```ruby
# bad
foo.to_s&.strip
foo.to_i&.zero?
foo.to_f&.zero?
foo.to_a&.size
foo.to_h&.size

# good
foo.to_s.strip
foo.to_i.zero?
foo.to_f.zero?
foo.to_a.size
foo.to_h.size
```

These conversion methods don't return `nil`.
@bbatsov bbatsov merged commit 3b87ec4 into rubocop:master Jul 20, 2025
24 checks passed
@koic koic deleted the make_lint_redundant_safe_navigation_aware_of_builtin_methods branch July 20, 2025 05:04
viralpraxis added a commit to viralpraxis/rubocop that referenced this pull request Jul 20, 2025
koic added a commit that referenced this pull request Jul 20, 2025
5hun-s pushed a commit to 5hun-s/rubocop that referenced this pull request Jul 21, 2025
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.

2 participants