Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix #10230] Fix a false positive for Lint/AmbiguousRange #10232

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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