Class: RuboCop::Cop::Lint::UselessOr
- 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.
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
Instance Attribute Summary
Attributes inherited from Base
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
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
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 |