From ee7ab6dceafa726d3ce936746d10d3eb50e27356 Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Thu, 4 Nov 2021 19:26:56 +0900 Subject: [PATCH] [Fix #10230] Fix a false positive for `Lint/AmbiguousRange` Fixes #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. --- changelog/fix_a_false_positive_for_lint_ambiguous_range.md | 1 + lib/rubocop/cop/lint/ambiguous_range.rb | 4 ++-- spec/rubocop/cop/lint/ambiguous_range_spec.rb | 7 ++++++- 3 files changed, 9 insertions(+), 3 deletions(-) create mode 100644 changelog/fix_a_false_positive_for_lint_ambiguous_range.md diff --git a/changelog/fix_a_false_positive_for_lint_ambiguous_range.md b/changelog/fix_a_false_positive_for_lint_ambiguous_range.md new file mode 100644 index 00000000000..3e40f16e13d --- /dev/null +++ b/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][]) diff --git a/lib/rubocop/cop/lint/ambiguous_range.rb b/lib/rubocop/cop/lint/ambiguous_range.rb index b6034626a6a..d806067f522 100644 --- a/lib/rubocop/cop/lint/ambiguous_range.rb +++ b/lib/rubocop/cop/lint/ambiguous_range.rb @@ -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 @@ -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 diff --git a/spec/rubocop/cop/lint/ambiguous_range_spec.rb b/spec/rubocop/cop/lint/ambiguous_range_spec.rb index f75a7554013..564b0467052 100644 --- a/spec/rubocop/cop/lint/ambiguous_range_spec.rb +++ b/spec/rubocop/cop/lint/ambiguous_range_spec.rb @@ -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