Class: RuboCop::Cop::Lint::LiteralAsCondition
- Inherits:
-
Base
- Object
- Base
- RuboCop::Cop::Lint::LiteralAsCondition
show all
- 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.'
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?, #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_rails_version, #target_ruby_version
#exclude_limit
#autocorrect?, #autocorrect_enabled?, #autocorrect_requested?, #autocorrect_with_disable_uncorrectable?, #correctable?, #disable_uncorrectable?, #safe_autocorrect?
#ignore_node, #ignored_node?, #part_of_ignored_node?
Methods included from Util
silence_warnings
Instance Method Details
#message(node) ⇒ Object
93
94
95
|
# File 'lib/rubocop/cop/lint/literal_as_condition.rb', line 93
def message(node)
format(MSG, literal: node.source)
end
|
#on_case(case_node) ⇒ Object
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
# File 'lib/rubocop/cop/lint/literal_as_condition.rb', line 58
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
73
74
75
76
77
78
79
80
81
82
83
84
85
|
# File 'lib/rubocop/cop/lint/literal_as_condition.rb', line 73
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
40
41
42
|
# File 'lib/rubocop/cop/lint/literal_as_condition.rb', line 40
def on_if(node)
check_for_literal(node)
end
|
#on_send(node) ⇒ Object
87
88
89
90
91
|
# File 'lib/rubocop/cop/lint/literal_as_condition.rb', line 87
def on_send(node)
return unless node.negation_method?
check_for_literal(node)
end
|
#on_until(node) ⇒ Object
Also known as:
on_until_post
51
52
53
54
55
|
# File 'lib/rubocop/cop/lint/literal_as_condition.rb', line 51
def on_until(node)
return if condition(node).false_type?
check_for_literal(node)
end
|
#on_while(node) ⇒ Object
Also known as:
on_while_post
44
45
46
47
48
|
# File 'lib/rubocop/cop/lint/literal_as_condition.rb', line 44
def on_while(node)
return if condition(node).true_type?
check_for_literal(node)
end
|