class Rubocop::Cop::Lint::LiteralInCondition
This cop checks for literals used as the conditions or as operands in and/or expressions serving as the conditions of if/while/until.
@example
if 20 do_something end if some_var && true do_something end
Constants
- LITERALS
- MSG
Public Instance Methods
on_if(node)
click to toggle source
Calls superclass method
# File lib/rubocop/cop/lint/literal_in_condition.rb, line 26 def on_if(node) check_for_literal(node) super end
on_until(node)
click to toggle source
Calls superclass method
# File lib/rubocop/cop/lint/literal_in_condition.rb, line 44 def on_until(node) check_for_literal(node) super end
on_until_post(node)
click to toggle source
Calls superclass method
# File lib/rubocop/cop/lint/literal_in_condition.rb, line 50 def on_until_post(node) check_for_literal(node) super end
on_while(node)
click to toggle source
Calls superclass method
# File lib/rubocop/cop/lint/literal_in_condition.rb, line 32 def on_while(node) check_for_literal(node) super end
on_while_post(node)
click to toggle source
Calls superclass method
# File lib/rubocop/cop/lint/literal_in_condition.rb, line 38 def on_while_post(node) check_for_literal(node) super end
Private Instance Methods
check_for_literal(node)
click to toggle source
# File lib/rubocop/cop/lint/literal_in_condition.rb, line 58 def check_for_literal(node) cond, = *node # if the code node is literal we obviously have a problem if LITERALS.include?(cond.type) add_offence(:warning, cond.loc.expression, format(MSG, cond.loc.expression.source)) elsif [:and, :or].include?(cond.type) # alternatively we have to consider a logical node with a # literal argument *operands = *cond operands.each do |op| if LITERALS.include?(op.type) add_offence(:warning, op.loc.expression, format(MSG, op.loc.expression.source)) end end end end