class RubyLint::Analysis::UselessEqualityChecks

Analysis class that checks for equality checks and adds warnings when these always evaluate to false. An example of such a check is `:hello == 'hello'`.

This analysis class was added after a co-worker banged their head against a wall after finding the following code:

if some_value.to_sym == "overall"
  # ...
end

This code would never evaluate to true. In the particular case this would result in it never caching certain data.

Public Instance Methods

definition_type(object) click to toggle source

Returns the type name of the given definition.

@param [RubyLint::Definition::RubyObject] object @return [String]

# File lib/ruby-lint/analysis/useless_equality_checks.rb, line 67
def definition_type(object)
  name = nil

  # Built-in types such as Array.
  if object.ruby_class
    name = object.ruby_class

  # Variables
  elsif object.variable? and object.value
    name = definition_type(object.value)

  # Constants.
  elsif object.constant? and !object.name.empty?
    name = object.name
  end

  return name
end
node_type(node) click to toggle source

Returns the type name for a given AST node.

@param [RubyLint::AST::Node] node @return [String]

# File lib/ruby-lint/analysis/useless_equality_checks.rb, line 50
def node_type(node)
  definition = vm.associations[node]
  type       = nil

  if definition
    type = definition_type(definition)
  end

  return type
end
on_send(node) click to toggle source

@param [RubyLint::AST::Node] node

# File lib/ruby-lint/analysis/useless_equality_checks.rb, line 24
def on_send(node)
  receiver, name, arg = *node

  return unless name == :==

  left_type  = node_type(receiver)
  right_type = node_type(arg)

  if skip_type?(left_type) or skip_type?(right_type)
    return
  end

  if left_type != right_type
    warning(
      "Comparing #{left_type} with #{right_type} evaluates to false",
      node
    )
  end
end
skip_type?(type) click to toggle source

@param [String] type

# File lib/ruby-lint/analysis/useless_equality_checks.rb, line 89
def skip_type?(type)
  return !type || type == 'unknown'
end