From b806ff29f10702401ffd9ea3fd7d5a2d10391215 Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Mon, 8 Aug 2022 09:16:12 +0900 Subject: [PATCH] [Fix #10552] Fix a false positive for `Lint/OutOfRangeRegexpRef` Follow up https://github.com/rubocop/rubocop-ast/pull/230 and fixes #10552. This commit fixes a false positive for `Lint/OutOfRangeRegexpRef` when using fixed-encoding regopt. --- ...x_a_false_positive_for_lint_out_of_range_regexp_ref.md | 1 + rubocop.gemspec | 2 +- spec/rubocop/cop/lint/out_of_range_regexp_ref_spec.rb | 8 ++++++++ 3 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 changelog/fix_a_false_positive_for_lint_out_of_range_regexp_ref.md diff --git a/changelog/fix_a_false_positive_for_lint_out_of_range_regexp_ref.md b/changelog/fix_a_false_positive_for_lint_out_of_range_regexp_ref.md new file mode 100644 index 00000000000..b67d128a49c --- /dev/null +++ b/changelog/fix_a_false_positive_for_lint_out_of_range_regexp_ref.md @@ -0,0 +1 @@ +* [#10552](https://github.com/rubocop/rubocop/issues/10552): Require RuboCop AST 1.20.0+ to fix a false positive for `Lint/OutOfRangeRegexpRef` when using fixed-encoding regopt. ([@koic][]) diff --git a/rubocop.gemspec b/rubocop.gemspec index d44afb44c90..0146ac7aaed 100644 --- a/rubocop.gemspec +++ b/rubocop.gemspec @@ -37,7 +37,7 @@ Gem::Specification.new do |s| s.add_runtime_dependency('rainbow', '>= 2.2.2', '< 4.0') s.add_runtime_dependency('regexp_parser', '>= 1.8', '< 3.0') s.add_runtime_dependency('rexml', '>= 3.2.5', '< 4.0') - s.add_runtime_dependency('rubocop-ast', '>= 1.19.1', '< 2.0') + s.add_runtime_dependency('rubocop-ast', '>= 1.20.0', '< 2.0') s.add_runtime_dependency('ruby-progressbar', '~> 1.7') s.add_runtime_dependency('unicode-display_width', '>= 1.4.0', '< 3.0') diff --git a/spec/rubocop/cop/lint/out_of_range_regexp_ref_spec.rb b/spec/rubocop/cop/lint/out_of_range_regexp_ref_spec.rb index f075eeecef3..32f9edc3c66 100644 --- a/spec/rubocop/cop/lint/out_of_range_regexp_ref_spec.rb +++ b/spec/rubocop/cop/lint/out_of_range_regexp_ref_spec.rb @@ -63,6 +63,14 @@ RUBY end + it 'does not register offense to a regexp with encoding option and valid references for numbered captures' do + expect_no_offenses(<<~RUBY) + /(foo)(bar)/u =~ "foobar" + puts $1 + puts $2 + RUBY + end + # RuboCop does not know a value of variables that it will contain in the regexp literal. # For example, `/(?#{var}*)` is interpreted as `/(?*)`. # So it does not offense when variables are used in regexp literals.