Parent

Files

Class/Module Index [+]

Quicksearch

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

MSG
NIL_NODE

Public Instance Methods

on_def(node) click to toggle source
# File lib/rubocop/cop/style/non_nil_check.rb, line 30
def on_def(node)
  method_name, _args, body = *node
  process_method(method_name, body)
end
on_defs(node) click to toggle source
# File lib/rubocop/cop/style/non_nil_check.rb, line 35
def on_defs(node)
  _scope, method_name, _args, body = *node
  process_method(method_name, body)
end
on_send(node) click to toggle source
# File lib/rubocop/cop/style/non_nil_check.rb, line 40
def on_send(node)
  return if ignored_node?(node)
  receiver, method, args = *node

  return unless [:!=, :!].include?(method)

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

[Validate]

Generated with the Darkfish Rdoc Generator 2.