Skip to content

Commit

Permalink
Restyle example formatting for Style/NonNilCheck
Browse files Browse the repository at this point in the history
Follow up of #4880 (comment).

This PR is a change of document format for `Style/NonNilCheck` cop.
  • Loading branch information
koic authored and bbatsov committed Oct 27, 2019
1 parent 694d4a9 commit 2f69571
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 18 deletions.
30 changes: 21 additions & 9 deletions lib/rubocop/cop/style/non_nil_check.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,39 @@ module Cop
module Style
# This cop checks for non-nil checks, which are usually redundant.
#
# @example
# With `IncludeSemanticChanges` set to `false` by default, this cop
# does not report offenses for `!x.nil?` and does no changes that might
# change behavior.
#
# With `IncludeSemanticChanges` set to `true`, this cop reports offenses
# for `!x.nil?` and autocorrects that and `x != nil` to solely `x`, which
# is **usually** OK, but might change behavior.
#
# @example
# # bad
# if x != nil
# end
#
# # good (when not allowing semantic changes)
# # bad (when allowing semantic changes)
# if !x.nil?
# end
#
# # good (when allowing semantic changes)
# # good
# if x
# end
#
# Non-nil checks are allowed if they are the final nodes of predicate.
#
# # Non-nil checks are allowed if they are the final nodes of predicate.
# # good
# def signed_in?
# !current_user.nil?
# end
#
# @example IncludeSemanticChanges: false (default)
# # good
# if !x.nil?
# end
#
# @example IncludeSemanticChanges: true
# # bad
# if !x.nil?
# end
#
class NonNilCheck < Cop
def_node_matcher :not_equal_to_nil?, '(send _ :!= nil)'
def_node_matcher :unless_check?, '(if (send _ :nil?) ...)'
Expand Down
34 changes: 25 additions & 9 deletions manual/cops_style.md
Original file line number Diff line number Diff line change
Expand Up @@ -4410,12 +4410,13 @@ Enabled | Yes | Yes | 0.20 | 0.22
This cop checks for non-nil checks, which are usually redundant.
Non-nil checks are allowed if they are the final nodes of predicate.
With `IncludeSemanticChanges` set to `false` by default, this cop
does not report offenses for `!x.nil?` and does no changes that might
change behavior.
# good
def signed_in?
!current_user.nil?
end
With `IncludeSemanticChanges` set to `true`, this cop reports offenses
for `!x.nil?` and autocorrects that and `x != nil` to solely `x`, which
is **usually** OK, but might change behavior.
### Examples
Expand All @@ -4424,13 +4425,28 @@ Non-nil checks are allowed if they are the final nodes of predicate.
if x != nil
end
# good (when not allowing semantic changes)
# bad (when allowing semantic changes)
# good
if x
end
# Non-nil checks are allowed if they are the final nodes of predicate.
# good
def signed_in?
!current_user.nil?
end
```
#### IncludeSemanticChanges: false (default)
```ruby
# good
if !x.nil?
end
```
#### IncludeSemanticChanges: true
# good (when allowing semantic changes)
if x
```ruby
# bad
if !x.nil?
end
```
Expand Down

0 comments on commit 2f69571

Please sign in to comment.