From 1a5d07344bdd3758b5821435b10dc7bc10543bd2 Mon Sep 17 00:00:00 2001 From: fatkodima Date: Mon, 13 Jul 2020 03:34:41 +0300 Subject: [PATCH] Support autocorrect for `Lint/DisjunctiveAssignmentInConstructor` cop --- CHANGELOG.md | 1 + config/default.yml | 1 + docs/modules/ROOT/pages/cops_lint.adoc | 4 ++-- .../disjunctive_assignment_in_constructor.rb | 10 +++++++-- ...junctive_assignment_in_constructor_spec.rb | 21 +++++++++++++++++-- 5 files changed, 31 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b84785b30bf..2bd2cb7f678 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ ### New features * [#7333](https://github.com/rubocop-hq/rubocop/issues/7333): Add new `Style/RedundantFileExtensionInRequire` cop. ([@fatkodima][]) +* [#8316](https://github.com/rubocop-hq/rubocop/pull/8316): Support autocorrect for `Lint/DisjunctiveAssignmentInConstructor` cop. ([@fatkodima][]) * [#8242](https://github.com/rubocop-hq/rubocop/pull/8242): Internal profiling available with `bin/rubocop-profile` and rake tasks. ([@marcandre][]) * [#8295](https://github.com/rubocop-hq/rubocop/pull/8295): Add new `Style/ArrayCoercion` cop. ([@fatkodima][]) * [#8293](https://github.com/rubocop-hq/rubocop/pull/8293): Add new `Lint/DuplicateElsifCondition` cop. ([@fatkodima][]) diff --git a/config/default.yml b/config/default.yml index f3a87958b8d..6d6904557ce 100644 --- a/config/default.yml +++ b/config/default.yml @@ -1397,6 +1397,7 @@ Lint/DisjunctiveAssignmentInConstructor: Enabled: true Safe: false VersionAdded: '0.62' + VersionChanged: '0.88' Lint/DuplicateCaseCondition: Description: 'Do not repeat values in case conditionals.' diff --git a/docs/modules/ROOT/pages/cops_lint.adoc b/docs/modules/ROOT/pages/cops_lint.adoc index 7fa7577c45f..0f75164560f 100644 --- a/docs/modules/ROOT/pages/cops_lint.adoc +++ b/docs/modules/ROOT/pages/cops_lint.adoc @@ -539,9 +539,9 @@ OpenSSL::Digest.digest('SHA256', 'foo') | Enabled | No -| No +| Yes (Unsafe) | 0.62 -| - +| 0.88 |=== This cop checks constructors for disjunctive assignments that should diff --git a/lib/rubocop/cop/lint/disjunctive_assignment_in_constructor.rb b/lib/rubocop/cop/lint/disjunctive_assignment_in_constructor.rb index c5e8dd0a3b5..f74c0799ecf 100644 --- a/lib/rubocop/cop/lint/disjunctive_assignment_in_constructor.rb +++ b/lib/rubocop/cop/lint/disjunctive_assignment_in_constructor.rb @@ -22,7 +22,9 @@ module Lint # def initialize # @x = 1 # end - class DisjunctiveAssignmentInConstructor < Cop + class DisjunctiveAssignmentInConstructor < Base + extend AutoCorrector + MSG = 'Unnecessary disjunctive assignment. Use plain assignment.' def on_def(node) @@ -73,7 +75,11 @@ def check_body_lines(lines) # @param [Node] node a disjunctive assignment def check_disjunctive_assignment(node) lhs = node.child_nodes.first - add_offense(node, location: :operator) if lhs.ivasgn_type? + return unless lhs.ivasgn_type? + + add_offense(node.loc.operator) do |corrector| + corrector.replace(node.loc.operator, '=') + end end end end diff --git a/spec/rubocop/cop/lint/disjunctive_assignment_in_constructor_spec.rb b/spec/rubocop/cop/lint/disjunctive_assignment_in_constructor_spec.rb index 50be552a04b..a7808dc65cf 100644 --- a/spec/rubocop/cop/lint/disjunctive_assignment_in_constructor_spec.rb +++ b/spec/rubocop/cop/lint/disjunctive_assignment_in_constructor_spec.rb @@ -43,7 +43,7 @@ def initialize end context 'LHS is ivar' do - it 'registers an offense' do + it 'registers an offense and corrects' do expect_offense(<<~RUBY) class Banana def initialize @@ -52,10 +52,18 @@ def initialize end end RUBY + + expect_correction(<<~RUBY) + class Banana + def initialize + @delicious = true + end + end + RUBY end context 'constructor calls super after assignment' do - it 'registers an offense' do + it 'registers an offense and corrects' do expect_offense(<<~RUBY) class Banana def initialize @@ -65,6 +73,15 @@ def initialize end end RUBY + + expect_correction(<<~RUBY) + class Banana + def initialize + @delicious = true + super + end + end + RUBY end end