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

[View source]

164
165
166
# File 'lib/rubocop/cop/lint/literal_as_condition.rb', line 164

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

#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

#on_case(case_node) ⇒ Object

rubocop:enable Metrics/AbcSize

[View source]

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

      add_offense(range, message: message)
    end
  end
end

#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

#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

#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

#on_until(node) ⇒ Object

rubocop:enable Metrics/AbcSize

[View source]

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

#on_until_post(node) ⇒ Object

rubocop:disable Metrics/AbcSize

[View source]

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

#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

#on_while_post(node) ⇒ Object

rubocop:disable Metrics/AbcSize

[View source]

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