From 96457eee0db1212575025a2aac4c1fba90494f08 Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Thu, 14 Oct 2021 12:34:16 +0900 Subject: [PATCH] [Fix #10185] Fix a false positive for `Lint/AmbiguousRange` Fixes #10185. This PR fixes a false positive for `Lint/AmbiguousRange` when using `self` in a range literal. --- changelog/fix_false_positive_for_lint_ambiguous_range.md | 1 + lib/rubocop/cop/lint/ambiguous_range.rb | 2 +- spec/rubocop/cop/lint/ambiguous_range_spec.rb | 7 +++++++ 3 files changed, 9 insertions(+), 1 deletion(-) create mode 100644 changelog/fix_false_positive_for_lint_ambiguous_range.md diff --git a/changelog/fix_false_positive_for_lint_ambiguous_range.md b/changelog/fix_false_positive_for_lint_ambiguous_range.md new file mode 100644 index 00000000000..fb831ac896c --- /dev/null +++ b/changelog/fix_false_positive_for_lint_ambiguous_range.md @@ -0,0 +1 @@ +* [#10185](https://github.com/rubocop/rubocop/issues/10185): Fix a false positive for `Lint/AmbiguousRange` when using `self` in a range literal. ([@koic][]) diff --git a/lib/rubocop/cop/lint/ambiguous_range.rb b/lib/rubocop/cop/lint/ambiguous_range.rb index e384b6f1130..b6034626a6a 100644 --- a/lib/rubocop/cop/lint/ambiguous_range.rb +++ b/lib/rubocop/cop/lint/ambiguous_range.rb @@ -82,7 +82,7 @@ def each_boundary(range) def acceptable?(node) node.begin_type? || node.basic_literal? || - node.variable? || node.const_type? || + 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 eb931156ce8..f75a7554013 100644 --- a/spec/rubocop/cop/lint/ambiguous_range_spec.rb +++ b/spec/rubocop/cop/lint/ambiguous_range_spec.rb @@ -74,6 +74,13 @@ RUBY end + it 'does not register an offense for `self`' do + expect_no_offenses(<<~RUBY) + self#{operator}42 + 42#{operator}self + RUBY + end + it 'can handle an endless range', :ruby26 do expect_offense(<<~RUBY) x || 1#{operator}