From e36f12090c53a87a3920a1831b1415ef995b35b9 Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Fri, 9 Oct 2020 00:45:26 +0900 Subject: [PATCH] [Fix #8862] Fix an error for `Lint/AmbiguousRegexpLiteral` Fixes #8862. This PR fixes an error for `Lint/AmbiguousRegexpLiteral` when using regexp without method calls in nested structure. --- CHANGELOG.md | 1 + .../cop/lint/ambiguous_regexp_literal.rb | 9 ++++++--- .../cop/lint/ambiguous_regexp_literal_spec.rb | 19 +++++++++++++++++++ 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f5e6801db8b..1f2ebe12ea3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ * [#8782](https://github.com/rubocop-hq/rubocop/issues/8782): Fix incorrect autocorrection for `Style/TernaryParentheses` with `defined?`. ([@dvandersluis][]) * [#8864](https://github.com/rubocop-hq/rubocop/issues/8864): Fix false positive for `Style/RedundantBegin` with a postfix `while` or `until`. ([@dvandersluis][]) * [#8869](https://github.com/rubocop-hq/rubocop/issues/8869): Fix a false positive for `Style/RedundantBegin` when using `begin` for or assignment and method call. ([@koic][]) +* [#8862](https://github.com/rubocop-hq/rubocop/issues/8862): Fix an error for `Lint/AmbiguousRegexpLiteral` when using regexp without method calls in nested structure. ([@koic][]) ## 0.93.0 (2020-10-08) diff --git a/lib/rubocop/cop/lint/ambiguous_regexp_literal.rb b/lib/rubocop/cop/lint/ambiguous_regexp_literal.rb index 3746f0a258f..c62b99f9d01 100644 --- a/lib/rubocop/cop/lint/ambiguous_regexp_literal.rb +++ b/lib/rubocop/cop/lint/ambiguous_regexp_literal.rb @@ -53,15 +53,18 @@ def find_offense_node_by(diagnostic) def find_offense_node(node, regexp_receiver) return node unless node.parent - if node.parent.send_type? || method_chain_to_regexp_receiver?(node) + if node.parent.send_type? || method_chain_to_regexp_receiver?(node, regexp_receiver) node = find_offense_node(node.parent, regexp_receiver) end node end - def method_chain_to_regexp_receiver?(node) - node.parent.parent && node.parent.receiver.receiver == regexp_receiver + def method_chain_to_regexp_receiver?(node, regexp_receiver) + return false unless (parent = node.parent) + return false unless (parent_receiver = parent.receiver) + + parent.parent && parent_receiver.receiver == regexp_receiver end end end diff --git a/spec/rubocop/cop/lint/ambiguous_regexp_literal_spec.rb b/spec/rubocop/cop/lint/ambiguous_regexp_literal_spec.rb index 7d60bf7efe2..020cbaa0d2d 100644 --- a/spec/rubocop/cop/lint/ambiguous_regexp_literal_spec.rb +++ b/spec/rubocop/cop/lint/ambiguous_regexp_literal_spec.rb @@ -60,6 +60,25 @@ RUBY end + it 'registers an offense and corrects when using regexp without method call in a nested structure' do + expect_offense(<<~RUBY) + class MyTest + test '#foo' do + assert_match /expected/, actual + ^ Ambiguous regexp literal. Parenthesize the method arguments if it's surely a regexp literal, or add a whitespace to the right of the `/` if it should be a division. + end + end + RUBY + + expect_correction(<<~RUBY) + class MyTest + test '#foo' do + assert_match(/expected/, actual) + end + end + RUBY + end + it 'registers an offense and corrects when using block argument' do expect_offense(<<~RUBY) p /pattern/, foo do |arg|