class RuboCop::Cop::Style::NonNilCheck

This cop checks for non-nil checks, which are usually redundant.

@example

# bad
if x != nil

# good (when not allowing semantic changes)
# bad (when allowing semantic changes)
if !x.nil?

# good (when allowing semantic changes)
if x

Non-nil checks are allowed if they are the final nodes of predicate.

# good
def signed_in?
  !current_user.nil?
end

Constants

NIL_NODE

Public Instance Methods

on_send(node) click to toggle source
# File lib/rubocop/cop/style/non_nil_check.rb, line 31
def on_send(node)
  return if ignored_node?(node)
  receiver, method, args = *node

  if method == :!=
    add_offense(node, :selector) if args == NIL_NODE
  elsif method == :! && include_semantic_changes?
    add_offense(node, :expression) if nil_check?(receiver)
  end
end

Private Instance Methods

autocorrect(node) click to toggle source
# File lib/rubocop/cop/style/non_nil_check.rb, line 76
def autocorrect(node)
  receiver, method, _args = *node

  if method == :!=
    autocorrect_comparison(node)
  elsif method == :!
    autocorrect_non_nil(node, receiver)
  end
end
autocorrect_comparison(node) click to toggle source
# File lib/rubocop/cop/style/non_nil_check.rb, line 86
def autocorrect_comparison(node)
  expr = node.source

  new_code =
    if include_semantic_changes?
      expr.sub(/\s*!=\s*nil/, '')
    else
      expr.sub(/^(\S*)\s*!=\s*nil/, '!\1.nil?')
    end

  return if expr == new_code

  lambda do |corrector|
    corrector.replace(node.source_range, new_code)
  end
end
autocorrect_non_nil(node, inner_node) click to toggle source
# File lib/rubocop/cop/style/non_nil_check.rb, line 103
def autocorrect_non_nil(node, inner_node)
  lambda do |corrector|
    receiver, _method, _args = *inner_node
    if receiver
      corrector.replace(node.source_range, receiver.source)
    else
      corrector.replace(node.source_range, 'self')
    end
  end
end
include_semantic_changes?() click to toggle source
# File lib/rubocop/cop/style/non_nil_check.rb, line 53
def include_semantic_changes?
  cop_config['IncludeSemanticChanges']
end
message(node) click to toggle source
# File lib/rubocop/cop/style/non_nil_check.rb, line 44
def message(node)
  _receiver, method, _args = *node
  if method == :!=
    'Prefer `!expression.nil?` over `expression != nil`.'
  else
    'Explicit non-nil checks are usually redundant.'
  end
end
nil_check?(node) click to toggle source
# File lib/rubocop/cop/style/non_nil_check.rb, line 69
def nil_check?(node)
  return false unless node && node.type == :send

  _receiver, method, *_args = *node
  method == :nil?
end
on_method_def(_node, name, _args, body) click to toggle source
# File lib/rubocop/cop/style/non_nil_check.rb, line 57
def on_method_def(_node, name, _args, body)
  # only predicate methods are handled differently
  return unless name.to_s.end_with?('?')
  return unless body

  if body.type != :begin
    ignore_node(body)
  elsif body.type == :begin
    ignore_node(body.children.last)
  end
end