Class: RuboCop::Cop::Lint::UselessOr

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

Overview

Checks for useless OR (‘||` and `or`) expressions.

Some methods always return a truthy value, even when called on ‘nil` (e.g. `nil.to_i` evaluates to `0`). Therefore, OR expressions appended after these methods will never evaluate.

Examples:


# bad
x.to_a || fallback
x.to_c || fallback
x.to_d || fallback
x.to_i || fallback
x.to_f || fallback
x.to_h || fallback
x.to_r || fallback
x.to_s || fallback
x.to_sym || fallback
x.intern || fallback
x.inspect || fallback
x.hash || fallback
x.object_id || fallback
x.__id__ || fallback

x.to_s or fallback

# good - if fallback is same as return value of method called on nil
x.to_a # nil.to_a returns []
x.to_c # nil.to_c returns (0+0i)
x.to_d # nil.to_d returns 0.0
x.to_i # nil.to_i returns 0
x.to_f # nil.to_f returns 0.0
x.to_h # nil.to_h returns {}
x.to_r # nil.to_r returns (0/1)
x.to_s # nil.to_s returns ''
x.to_sym # nil.to_sym raises an error
x.intern # nil.intern raises an error
x.inspect # nil.inspect returns "nil"
x.hash # nil.hash returns an Integer
x.object_id # nil.object_id returns an Integer
x.__id__ # nil.object_id returns an Integer

# good - if the intention is not to call the method on nil
x&.to_a || fallback
x&.to_c || fallback
x&.to_d || fallback
x&.to_i || fallback
x&.to_f || fallback
x&.to_h || fallback
x&.to_r || fallback
x&.to_s || fallback
x&.to_sym || fallback
x&.intern || fallback
x&.inspect || fallback
x&.hash || fallback
x&.object_id || fallback
x&.__id__ || fallback

x&.to_s or fallback

Constant Summary collapse

MSG =
'`%<rhs>s` will never evaluate because `%<lhs>s` always returns a truthy value.'
TRUTHY_RETURN_VALUE_METHODS =
Set[:to_a, :to_c, :to_d, :to_i, :to_f, :to_h, :to_r,
:to_s, :to_sym, :intern, :inspect, :hash, :object_id,
:__id__].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_gem_version, #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_or(node) ⇒ Object



78
79
80
81
82
83
84
85
86
87
# File 'lib/rubocop/cop/lint/useless_or.rb', line 78

def on_or(node)
  if truthy_return_value_method?(node.lhs)
    report_offense(node, node.lhs)
  elsif truthy_return_value_method?(node.rhs)
    parent = node.parent
    parent = parent.parent if parent&.begin_type?

    report_offense(parent, node.rhs) if parent&.or_type?
  end
end

#truthy_return_value_method?(node) ⇒ Object



74
75
76
# File 'lib/rubocop/cop/lint/useless_or.rb', line 74

def_node_matcher :truthy_return_value_method?, <<~PATTERN
  (send _ %TRUTHY_RETURN_VALUE_METHODS)
PATTERN