Skip to content

Commit

Permalink
[Fix rubocop#10230] Fix a false positive for Lint/AmbiguousRange
Browse files Browse the repository at this point in the history
Fixes rubocop#10230.

This PR fixes a false positive for `Lint/AmbiguousRange`
when a range is composed of all literals except basic literals.
If it's just literals, it's clear even without parentheses,
so this PR will accept them.
  • Loading branch information
koic committed Nov 12, 2021
1 parent 51bac9d commit e2ec786
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
1 change: 1 addition & 0 deletions changelog/fix_a_false_positive_for_lint_ambiguous_range.md
@@ -0,0 +1 @@
* [#10230](https://github.com/rubocop/rubocop/issues/10230): Fix a false positive for `Lint/AmbiguousRange` when a range is composed of all literals except basic literals. ([@koic][])
4 changes: 2 additions & 2 deletions lib/rubocop/cop/lint/ambiguous_range.rb
Expand Up @@ -8,7 +8,7 @@ module Lint
# Ranges have quite low precedence, which leads to unexpected behaviour 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 basic literal: numerics, strings, symbols, etc.).
# 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
Expand Down Expand Up @@ -81,7 +81,7 @@ def each_boundary(range)

def acceptable?(node)
node.begin_type? ||
node.basic_literal? ||
node.literal? ||
node.variable? || node.const_type? || node.self_type? ||
(node.call_type? && acceptable_call?(node))
end
Expand Down
7 changes: 6 additions & 1 deletion spec/rubocop/cop/lint/ambiguous_range_spec.rb
Expand Up @@ -55,10 +55,15 @@
RUBY
end

it 'does not register an offense if the range is composed of basic literals' do
it 'does not register an offense if the range is composed of literals' do
expect_no_offenses(<<~RUBY)
1#{operator}2
'a'#{operator}'z'
"\#{foo}-\#{bar}"#{operator}'123-4567'
`date`#{operator}'foobar'
:"\#{foo}-\#{bar}"#{operator}:baz
/a/#{operator}/b/
42#{operator}nil
RUBY
end

Expand Down

0 comments on commit e2ec786

Please sign in to comment.