Class: RuboCop::Cop::Lint::UselessDefined

Inherits:
Base
  • Object
show all
Defined in:
lib/rubocop/cop/lint/useless_defined.rb

Overview

Checks for calls to ‘defined?` with strings or symbols as the argument. Such calls will always return `’expression’‘, you probably meant to check for the existence of a constant, method, or variable instead.

‘defined?` is part of the Ruby syntax and doesn’t behave like normal methods. You can safely pass in what you are checking for directly, without encountering a ‘NameError`.

When interpolation is used, oftentimes it is not possible to write the code with ‘defined?`. In these cases, switch to one of the more specific methods:

  • ‘class_variable_defined?`

  • ‘const_defined?`

  • ‘method_defined?`

  • ‘instance_variable_defined?`

  • ‘binding.local_variable_defined?`

Examples:


# bad
defined?('FooBar')
defined?(:FooBar)
defined?(:foo_bar)
defined?('foo_bar')

# good
defined?(FooBar)
defined?(foo_bar)

# bad - interpolation
bar = 'Bar'
defined?("Foo::#{bar}::Baz")

# good
bar = 'Bar'
defined?(Foo) && Foo.const_defined?(bar) && Foo.const_get(bar).const_defined?(:Baz)

Constant Summary collapse

MSG =
'Calling `defined?` with a %<type>s argument will always return a truthy value.'
TYPES =
{ str: 'string', dstr: 'string', sym: 'symbol', dsym: 'symbol' }.freeze

Constants inherited from Base

Base::RESTRICT_ON_SEND

Instance Attribute Summary

Attributes inherited from Base

#config, #processed_source

Instance Method Summary collapse

Methods inherited from Base

#active_support_extensions_enabled?, #add_global_offense, #add_offense, #always_autocorrect?, autocorrect_incompatible_with, badge, #begin_investigation, #callbacks_needed, callbacks_needed, #config_to_allow_offenses, #config_to_allow_offenses=, #contextual_autocorrect?, #cop_config, cop_name, #cop_name, department, documentation_url, exclude_from_registry, #excluded_file?, #external_dependency_checksum, inherited, #initialize, #inspect, joining_forces, lint?, match?, #message, #offenses, #on_investigation_end, #on_new_investigation, #on_other_file, #parse, #parser_engine, #ready, #relevant_file?, requires_gem, #string_literals_frozen_by_default?, support_autocorrect?, support_multiple_source?, #target_rails_version, #target_ruby_version

Methods included from ExcludeLimit

#exclude_limit

Methods included from AutocorrectLogic

#autocorrect?, #autocorrect_enabled?, #autocorrect_requested?, #autocorrect_with_disable_uncorrectable?, #correctable?, #disable_uncorrectable?, #safe_autocorrect?

Methods included from IgnoredNode

#ignore_node, #ignored_node?, #part_of_ignored_node?

Methods included from Util

silence_warnings

Constructor Details

This class inherits a constructor from RuboCop::Cop::Base

Instance Method Details

#on_defined?(node) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
49
50
51
# File 'lib/rubocop/cop/lint/useless_defined.rb', line 46

def on_defined?(node)
  # NOTE: `defined?` always takes one argument. Anything else is a syntax error.
  return unless (type = TYPES[node.first_argument.type])

  add_offense(node, message: format(MSG, type: type))
end