Class: RuboCop::Cop::Lint::LiteralAsCondition

Inherits:
Base
  • Object
show all
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.

Examples:


# bad
if 20
  do_something
end

# bad
# We're only interested in the left hand side being a truthy literal,
# because it affects the evaluation of the &&, whereas the right hand
# side will be conditionally executed/called and can be a literal.
if true && some_var
  do_something
end

# good
if some_var
  do_something
end

# good
# When using a boolean value for an infinite loop.
while true
  break if condition
end

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

#config, #processed_source

Instance Method Summary collapse

Methods included from AutoCorrector

support_autocorrect?

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

#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

#message(node) ⇒ Object



174
175
176
# File 'lib/rubocop/cop/lint/literal_as_condition.rb', line 174

def message(node)
  format(MSG, literal: node.source)
end

#on_and(node) ⇒ Object



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

#on_case(case_node) ⇒ Object

rubocop:enable Metrics/AbcSize



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/rubocop/cop/lint/literal_as_condition.rb', line 137

def on_case(case_node)
  if (cond = case_node.condition)
    return if !cond.falsey_literal? && !cond.truthy_literal?

    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)
      message = message(range)

      add_offense(range, message: message)
    end
  end
end

#on_case_match(case_match_node) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/rubocop/cop/lint/literal_as_condition.rb', line 154

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

#on_if(node) ⇒ Object



69
70
71
72
73
74
75
# File 'lib/rubocop/cop/lint/literal_as_condition.rb', line 69

def on_if(node)
  cond = condition(node)

  return unless cond.falsey_literal? || cond.truthy_literal?

  correct_if_node(node, cond)
end

#on_or(node) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/rubocop/cop/lint/literal_as_condition.rb', line 57

def on_or(node)
  return unless node.lhs.falsey_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

#on_send(node) ⇒ Object



168
169
170
171
172
# File 'lib/rubocop/cop/lint/literal_as_condition.rb', line 168

def on_send(node)
  return unless node.negation_method?

  check_for_literal(node)
end

#on_until(node) ⇒ Object

rubocop:enable Metrics/AbcSize



107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/rubocop/cop/lint/literal_as_condition.rb', line 107

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

#on_until_post(node) ⇒ Object

rubocop:disable Metrics/AbcSize



122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/rubocop/cop/lint/literal_as_condition.rb', line 122

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

#on_while(node) ⇒ Object



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

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

#on_while_post(node) ⇒ Object

rubocop:disable Metrics/AbcSize



92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/rubocop/cop/lint/literal_as_condition.rb', line 92

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