Skip to content

Commit

Permalink
Merge pull request #8865 from jaredbeck/docs_redundant_safe_navigation
Browse files Browse the repository at this point in the history
Docs: RedundantSafeNavigation
  • Loading branch information
koic committed Oct 8, 2020
2 parents 87f5d45 + d7c0b3d commit db37bea
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
11 changes: 8 additions & 3 deletions docs/modules/ROOT/pages/cops_lint.adoc
Expand Up @@ -2918,14 +2918,19 @@ require 'unloaded_feature'
|===

This cop checks for redundant safe navigation calls.
It is marked as unsafe, because it can produce code that returns
non `nil` while `nil` result is expected on `nil` receiver.

In the example below, the safe navigation operator (`&.`) is unnecessary
because `NilClass` has methods like `respond_to?` and `dup`.

This cop is marked as unsafe, because auto-correction can change the
return type of the expression. An offending expression that previously
could return `nil` will be auto-corrected to never return `nil`.

=== Examples

[source,ruby]
----
# bad
# bad, because nil has these methods
attrs&.respond_to?(:[])
foo&.dup&.inspect
Expand Down
11 changes: 8 additions & 3 deletions lib/rubocop/cop/lint/redundant_safe_navigation.rb
Expand Up @@ -4,11 +4,16 @@ module RuboCop
module Cop
module Lint
# This cop checks for redundant safe navigation calls.
# It is marked as unsafe, because it can produce code that returns
# non `nil` while `nil` result is expected on `nil` receiver.
#
# In the example below, the safe navigation operator (`&.`) is unnecessary
# because `NilClass` has methods like `respond_to?` and `dup`.
#
# This cop is marked as unsafe, because auto-correction can change the
# return type of the expression. An offending expression that previously
# could return `nil` will be auto-corrected to never return `nil`.
#
# @example
# # bad
# # bad, because nil has these methods
# attrs&.respond_to?(:[])
# foo&.dup&.inspect
#
Expand Down

0 comments on commit db37bea

Please sign in to comment.