Class: RuboCop::Cop::Lint::LiteralAsCondition
- Extended by:
- AutoCorrector
- Includes:
- RangeHelp
- Defined in:
- lib/rubocop/cop/lint/literal_as_condition.rb
Overview
Checks for literals used as the conditions or as operands in and/or expressions serving as the conditions of if/while/until/case-when/case-in.
NOTE: Literals in ‘case-in` condition where the match variable is used in `in` are accepted as a pattern matching.
Constant Summary collapse
- MSG =
'Literal `%<literal>s` appeared as a condition.'
- RESTRICT_ON_SEND =
[:!].freeze
Constants included from RangeHelp
RangeHelp::BYTE_ORDER_MARK, RangeHelp::NOT_GIVEN
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
- #message(node) ⇒ Object
- #on_and(node) ⇒ Object
-
#on_case(case_node) ⇒ Object
rubocop:enable Metrics/AbcSize.
- #on_case_match(case_match_node) ⇒ Object
- #on_if(node) ⇒ Object
- #on_send(node) ⇒ Object
-
#on_until(node) ⇒ Object
rubocop:enable Metrics/AbcSize.
-
#on_until_post(node) ⇒ Object
rubocop:disable Metrics/AbcSize.
- #on_while(node) ⇒ Object
-
#on_while_post(node) ⇒ Object
rubocop:disable Metrics/AbcSize.
Methods included from AutoCorrector
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?, #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
permalink #message(node) ⇒ Object
[View source]
164 165 166 |
# File 'lib/rubocop/cop/lint/literal_as_condition.rb', line 164 def (node) format(MSG, literal: node.source) end |
permalink #on_and(node) ⇒ Object
[View source]
45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/rubocop/cop/lint/literal_as_condition.rb', line 45 def on_and(node) return unless node.lhs.truthy_literal? add_offense(node.lhs) do |corrector| # Don't autocorrect `'foo' && return` because having `return` as # the leftmost node can lead to a void value expression syntax error. next if node.rhs.type?(:return, :break, :next) corrector.replace(node, node.rhs.source) end end |
permalink #on_case(case_node) ⇒ Object
rubocop:enable Metrics/AbcSize
129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/rubocop/cop/lint/literal_as_condition.rb', line 129 def on_case(case_node) if case_node.condition check_case(case_node) else case_node.when_branches.each do |when_node| next unless when_node.conditions.all?(&:literal?) range = when_conditions_range(when_node) = (range) add_offense(range, message: ) end end end |
permalink #on_case_match(case_match_node) ⇒ Object
[View source]
144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/rubocop/cop/lint/literal_as_condition.rb', line 144 def on_case_match(case_match_node) if case_match_node.condition return if case_match_node.descendants.any?(&:match_var_type?) check_case(case_match_node) else case_match_node.each_in_pattern do |in_pattern_node| next unless in_pattern_node.condition.literal? add_offense(in_pattern_node) end end end |
permalink #on_if(node) ⇒ Object
[View source]
57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/rubocop/cop/lint/literal_as_condition.rb', line 57 def on_if(node) cond = condition(node) if node.unless? correct_if_node(node, cond, true) if cond.falsey_literal? correct_if_node(node, cond, false) if cond.truthy_literal? else correct_if_node(node, cond, true) if cond.truthy_literal? correct_if_node(node, cond, false) if cond.falsey_literal? end end |
permalink #on_send(node) ⇒ Object
[View source]
158 159 160 161 162 |
# File 'lib/rubocop/cop/lint/literal_as_condition.rb', line 158 def on_send(node) return unless node.negation_method? check_for_literal(node) end |
permalink #on_until(node) ⇒ Object
rubocop:enable Metrics/AbcSize
99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/rubocop/cop/lint/literal_as_condition.rb', line 99 def on_until(node) return if node.condition.source == 'false' if node.condition.falsey_literal? add_offense(node.condition) do |corrector| corrector.replace(node.condition, 'false') end elsif node.condition.truthy_literal? add_offense(node.condition) do |corrector| corrector.remove(node) end end end |
permalink #on_until_post(node) ⇒ Object
rubocop:disable Metrics/AbcSize
114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/rubocop/cop/lint/literal_as_condition.rb', line 114 def on_until_post(node) return if node.condition.source == 'false' if node.condition.falsey_literal? add_offense(node.condition) do |corrector| corrector.replace(node, node.source.sub(node.condition.source, 'false')) end elsif node.condition.truthy_literal? add_offense(node.condition) do |corrector| corrector.replace(node, node.body.child_nodes.map(&:source).join("\n")) end end end |
permalink #on_while(node) ⇒ Object
[View source]
69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/rubocop/cop/lint/literal_as_condition.rb', line 69 def on_while(node) return if node.condition.source == 'true' if node.condition.truthy_literal? add_offense(node.condition) do |corrector| corrector.replace(node.condition, 'true') end elsif node.condition.falsey_literal? add_offense(node.condition) do |corrector| corrector.remove(node) end end end |
permalink #on_while_post(node) ⇒ Object
rubocop:disable Metrics/AbcSize
84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/rubocop/cop/lint/literal_as_condition.rb', line 84 def on_while_post(node) return if node.condition.source == 'true' if node.condition.truthy_literal? add_offense(node.condition) do |corrector| corrector.replace(node, node.source.sub(node.condition.source, 'true')) end elsif node.condition.falsey_literal? add_offense(node.condition) do |corrector| corrector.replace(node, node.body.child_nodes.map(&:source).join("\n")) end end end |