class RuboCop::Cop::Lint::AmbiguousRange

Checks for ambiguous ranges.

Ranges have quite low precedence, which leads to unexpected behavior when using a range with other operators. This cop avoids that by making ranges explicit by requiring parenthesis around complex range boundaries (anything that is not a literal: numerics, strings, symbols, etc.).

This cop can be configured with ‘RequireParenthesesForMethodChains` in order to specify whether method chains (including `self.foo`) should be wrapped in parens by this cop.

NOTE: Regardless of this configuration, if a method receiver is a basic literal value, it will be wrapped in order to prevent the ambiguity of ‘1..2.to_a`.

@safety

The cop autocorrects by wrapping the entire boundary in parentheses, which
makes the outcome more explicit but is possible to not be the intention of the
programmer. For this reason, this cop's autocorrect is unsafe (it will not
change the behavior of the code, but will not necessarily match the
intent of the program).

@example

# bad
x || 1..2
(x || 1..2)
1..2.to_a

# good, unambiguous
1..2
'a'..'z'
:bar..:baz
MyClass::MIN..MyClass::MAX
@min..@max
a..b
-a..b

# good, ambiguity removed
x || (1..2)
(x || 1)..2
(x || 1)..(y || 2)
(1..2).to_a

@example RequireParenthesesForMethodChains: false (default)

# good
a.foo..b.bar
(a.foo)..(b.bar)

@example RequireParenthesesForMethodChains: true

# bad
a.foo..b.bar

# good
(a.foo)..(b.bar)

Constants

MSG

Public Instance Methods

on_erange(node)
Alias for: on_irange
on_irange(node) click to toggle source
# File lib/rubocop/cop/lint/ambiguous_range.rb, line 64
def on_irange(node)
  each_boundary(node) do |boundary|
    next if acceptable?(boundary)

    add_offense(boundary) do |corrector|
      corrector.wrap(boundary, '(', ')')
    end
  end
end
Also aliased as: on_erange

Private Instance Methods

acceptable?(node) click to toggle source
# File lib/rubocop/cop/lint/ambiguous_range.rb, line 82
def acceptable?(node)
  node.begin_type? ||
    node.literal? ||
    node.variable? || node.const_type? || node.self_type? ||
    (node.call_type? && acceptable_call?(node))
end
acceptable_call?(node) click to toggle source
# File lib/rubocop/cop/lint/ambiguous_range.rb, line 89
def acceptable_call?(node)
  return true if node.unary_operation?

  # Require parentheses when making a method call on a literal
  # to avoid the ambiguity of `1..2.to_a`.
  return false if node.receiver&.basic_literal?

  require_parentheses_for_method_chain? || node.receiver.nil?
end
each_boundary(range) { |begin| ... } click to toggle source
# File lib/rubocop/cop/lint/ambiguous_range.rb, line 77
def each_boundary(range)
  yield range.begin if range.begin
  yield range.end if range.end
end
require_parentheses_for_method_chain?() click to toggle source
# File lib/rubocop/cop/lint/ambiguous_range.rb, line 99
def require_parentheses_for_method_chain?
  !cop_config['RequireParenthesesForMethodChains']
end