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}