From ce67bd8b40153bdffd980bfd386d58d46af20799 Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Sat, 20 Jun 2020 07:07:26 +0900 Subject: [PATCH] Support autocorrection for `Lint/RegexpAsCondition` This PR supports autocorrection for `Lint/RegexpAsCondition`. --- CHANGELOG.md | 1 + config/default.yml | 1 + docs/modules/ROOT/pages/cops_lint.adoc | 4 ++-- lib/rubocop/cop/lint/regexp_as_condition.rb | 6 ++++++ spec/rubocop/cop/lint/regexp_as_condition_spec.rb | 7 ++++++- 5 files changed, 16 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e37b805f504..f90245a5b18 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ * [#8148](https://github.com/rubocop-hq/rubocop/pull/8148): Support autocorrection for `Style/MultilineTernaryOperator`. ([@koic][]) * [#8151](https://github.com/rubocop-hq/rubocop/pull/8151): Support autocorrection for `Style/NestedTernaryOperator`. ([@koic][]) * [#8142](https://github.com/rubocop-hq/rubocop/pull/8142): Add `Lint/ConstantResolution` cop. ([@robotdana][]) +* [#8170](https://github.com/rubocop-hq/rubocop/pull/8170): Support autocorrection for `Lint/RegexpAsCondition`. ([@koic][]) ### Bug fixes diff --git a/config/default.yml b/config/default.yml index ddfbaa8eb22..19c9ffa270f 100644 --- a/config/default.yml +++ b/config/default.yml @@ -1677,6 +1677,7 @@ Lint/RegexpAsCondition: The regexp literal matches `$_` implicitly. Enabled: true VersionAdded: '0.51' + VersionChanged: '0.86' Lint/RequireParentheses: Description: >- diff --git a/docs/modules/ROOT/pages/cops_lint.adoc b/docs/modules/ROOT/pages/cops_lint.adoc index 925ec93c28b..fb6601b1781 100644 --- a/docs/modules/ROOT/pages/cops_lint.adoc +++ b/docs/modules/ROOT/pages/cops_lint.adoc @@ -2524,9 +2524,9 @@ end | Enabled | Yes -| No +| Yes | 0.51 -| - +| 0.86 |=== This cop checks for regexp literals used as `match-current-line`. diff --git a/lib/rubocop/cop/lint/regexp_as_condition.rb b/lib/rubocop/cop/lint/regexp_as_condition.rb index 3a06a929948..9ffdbd403ca 100644 --- a/lib/rubocop/cop/lint/regexp_as_condition.rb +++ b/lib/rubocop/cop/lint/regexp_as_condition.rb @@ -23,6 +23,12 @@ class RegexpAsCondition < Cop def on_match_current_line(node) add_offense(node) end + + def autocorrect(node) + lambda do |corrector| + corrector.replace(node, "#{node.source} =~ $_") + end + end end end end diff --git a/spec/rubocop/cop/lint/regexp_as_condition_spec.rb b/spec/rubocop/cop/lint/regexp_as_condition_spec.rb index cda56d79a92..3a8d15abd5d 100644 --- a/spec/rubocop/cop/lint/regexp_as_condition_spec.rb +++ b/spec/rubocop/cop/lint/regexp_as_condition_spec.rb @@ -5,12 +5,17 @@ let(:config) { RuboCop::Config.new } - it 'registers an offense for a regexp literal in `if` condition' do + it 'registers an offense and corrects for a regexp literal in `if` condition' do expect_offense(<<~RUBY) if /foo/ ^^^^^ Do not use regexp literal as a condition. The regexp literal matches `$_` implicitly. end RUBY + + expect_correction(<<~RUBY) + if /foo/ =~ $_ + end + RUBY end it 'does not register an offense for a regexp literal outside conditions' do