From 0400bba5ac9d88b99130b50169040f92c8ed2f7e Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Wed, 16 Jun 2021 02:47:54 +0900 Subject: [PATCH] Fix a false positive for `Style/RegexpLiteral` This PR fixes the following false positive for `Style/RegexpLiteral` when using a regexp starts with a blank as a method argument. ```console % cat example.rb do_something %r| regexp| % bundle exec rubocop --only Style/RegexpLiteral -a (snip) Offenses: example.rb:1:14: C: [Corrected] Style/RegexpLiteral: Use // around regular expression. do_something %r| regexp| ^^^^^^^^^^^ 1 file inspected, 1 offense detected, 1 offense corrected % cat example.rb do_something / regexp/ % ruby -c example.rb example.rb:1: syntax error, unexpected end-of-input ``` --- ...false_positive_for_style_regexp_literal.md | 1 + lib/rubocop/cop/style/regexp_literal.rb | 5 ++- spec/rubocop/cop/style/regexp_literal_spec.rb | 38 +++++++++++++++++++ 3 files changed, 42 insertions(+), 2 deletions(-) create mode 100644 changelog/fix_a_false_positive_for_style_regexp_literal.md diff --git a/changelog/fix_a_false_positive_for_style_regexp_literal.md b/changelog/fix_a_false_positive_for_style_regexp_literal.md new file mode 100644 index 00000000000..24ea80f6cc8 --- /dev/null +++ b/changelog/fix_a_false_positive_for_style_regexp_literal.md @@ -0,0 +1 @@ +* [#9880](https://github.com/rubocop/rubocop/pull/9880): Fix a false positive for `Style/RegexpLiteral` when using a regexp starts with a blank as a method argument. ([@koic][]) diff --git a/lib/rubocop/cop/style/regexp_literal.rb b/lib/rubocop/cop/style/regexp_literal.rb index cffea6d3561..13bd1bb06ac 100644 --- a/lib/rubocop/cop/style/regexp_literal.rb +++ b/lib/rubocop/cop/style/regexp_literal.rb @@ -117,7 +117,7 @@ def allowed_mixed_slash?(node) def allowed_percent_r_literal?(node) style == :slashes && contains_disallowed_slash?(node) || style == :percent_r || - allowed_mixed_percent_r?(node) || omit_parentheses_style?(node) + allowed_mixed_percent_r?(node) || allowed_omit_parentheses_with_percent_r_literal?(node) end def allowed_mixed_percent_r?(node) @@ -149,8 +149,9 @@ def preferred_delimiters config.for_cop('Style/PercentLiteralDelimiters') ['PreferredDelimiters']['%r'].chars end - def omit_parentheses_style?(node) + def allowed_omit_parentheses_with_percent_r_literal?(node) return false unless node.parent&.call_type? + return true if node.content.start_with?(' ') enforced_style = config.for_cop('Style/MethodCallWithArgsParentheses')['EnforcedStyle'] diff --git a/spec/rubocop/cop/style/regexp_literal_spec.rb b/spec/rubocop/cop/style/regexp_literal_spec.rb index 5c4affe6c34..77c3178c1ee 100644 --- a/spec/rubocop/cop/style/regexp_literal_spec.rb +++ b/spec/rubocop/cop/style/regexp_literal_spec.rb @@ -518,6 +518,25 @@ ^^^^^^^^^^ Use `//` around regular expression. RUBY end + + it 'does not register an offense when using a regexp starts with a blank as a method argument' do + expect_no_offenses(<<~RUBY) + do_something %r/ regexp/ + RUBY + end + + it 'does not register an offense when using a regexp starts with a blank as a safe navigation method argument' do + expect_no_offenses(<<~RUBY) + foo&.do_something %r/ regexp/ + RUBY + end + + it 'registers an offense when using a regexp starts with a blank' do + expect_offense(<<~RUBY) + %r/ regexp/ + ^^^^^^^^^^^ Use `//` around regular expression. + RUBY + end end context 'when using `%r` regexp with `EnforcedStyle: mixed`' do @@ -543,6 +562,25 @@ ^^^^^^^^^^ Use `//` around regular expression. RUBY end + + it 'does not register an offense when using a regexp starts with a blank as a method argument' do + expect_no_offenses(<<~RUBY) + do_something %r/ regexp/ + RUBY + end + + it 'does not register an offense when using a regexp starts with a blank as a safe navigation method argument' do + expect_no_offenses(<<~RUBY) + foo&.do_something %r/ regexp/ + RUBY + end + + it 'registers an offense when using a regexp starts with a blank' do + expect_offense(<<~RUBY) + %r/ regexp/ + ^^^^^^^^^^^ Use `//` around regular expression. + RUBY + end end end