Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new
Lint/SelfAssignment
cop (#8387)
Co-authored-by: Bozhidar Batsov <bozhidar@batsov.com>
- Loading branch information
Showing
7 changed files
with
243 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Lint | ||
# This cop checks for self-assignments. | ||
# | ||
# @example | ||
# # bad | ||
# foo = foo | ||
# foo, bar = foo, bar | ||
# Foo = Foo | ||
# | ||
# # good | ||
# foo = bar | ||
# foo, bar = bar, foo | ||
# Foo = Bar | ||
# | ||
class SelfAssignment < Base | ||
MSG = 'Self-assignment detected.' | ||
|
||
ASSIGNMENT_TYPE_TO_RHS_TYPE = { | ||
lvasgn: :lvar, | ||
ivasgn: :ivar, | ||
cvasgn: :cvar, | ||
gvasgn: :gvar | ||
}.freeze | ||
|
||
def on_lvasgn(node) | ||
lhs, rhs = *node | ||
return unless rhs | ||
|
||
rhs_type = ASSIGNMENT_TYPE_TO_RHS_TYPE[node.type] | ||
|
||
add_offense(node) if rhs.type == rhs_type && rhs.source == lhs.to_s | ||
end | ||
alias on_ivasgn on_lvasgn | ||
alias on_cvasgn on_lvasgn | ||
alias on_gvasgn on_lvasgn | ||
|
||
def on_casgn(node) | ||
lhs_scope, lhs_name, rhs = *node | ||
return unless rhs.const_type? | ||
|
||
rhs_scope, rhs_name = *rhs | ||
add_offense(node) if lhs_scope == rhs_scope && lhs_name == rhs_name | ||
end | ||
|
||
def on_masgn(node) | ||
add_offense(node) if multiple_self_assignment?(node) | ||
end | ||
|
||
def on_or_asgn(node) | ||
lhs, rhs = *node | ||
add_offense(node) if rhs_matches_lhs?(rhs, lhs) | ||
end | ||
alias on_and_asgn on_or_asgn | ||
|
||
private | ||
|
||
def multiple_self_assignment?(node) | ||
lhs, rhs = *node | ||
return false unless rhs.array_type? | ||
return false unless lhs.children.size == rhs.children.size | ||
|
||
lhs.children.zip(rhs.children).all? do |lhs_item, rhs_item| | ||
rhs_matches_lhs?(rhs_item, lhs_item) | ||
end | ||
end | ||
|
||
def rhs_matches_lhs?(rhs, lhs) | ||
rhs.type == ASSIGNMENT_TYPE_TO_RHS_TYPE[lhs.type] && | ||
rhs.children.first == lhs.children.first | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Lint::SelfAssignment do | ||
subject(:cop) { described_class.new } | ||
|
||
it 'registers an offense when using local var self-assignment' do | ||
expect_offense(<<~RUBY) | ||
foo = foo | ||
^^^^^^^^^ Self-assignment detected. | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using local var assignment' do | ||
expect_no_offenses(<<~RUBY) | ||
foo = bar | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using instance var self-assignment' do | ||
expect_offense(<<~RUBY) | ||
@foo = @foo | ||
^^^^^^^^^^^ Self-assignment detected. | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using instance var assignment' do | ||
expect_no_offenses(<<~RUBY) | ||
@foo = @bar | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using class var self-assignment' do | ||
expect_offense(<<~RUBY) | ||
@@foo = @@foo | ||
^^^^^^^^^^^^^ Self-assignment detected. | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using class var assignment' do | ||
expect_no_offenses(<<~RUBY) | ||
@@foo = @@bar | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using global var self-assignment' do | ||
expect_offense(<<~RUBY) | ||
$foo = $foo | ||
^^^^^^^^^^^ Self-assignment detected. | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using global var assignment' do | ||
expect_no_offenses(<<~RUBY) | ||
$foo = $bar | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using constant var self-assignment' do | ||
expect_offense(<<~RUBY) | ||
Foo = Foo | ||
^^^^^^^^^ Self-assignment detected. | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using constant var assignment for constant from another scope' do | ||
expect_no_offenses(<<~RUBY) | ||
Foo = ::Foo | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using multiple var self-assignment' do | ||
expect_offense(<<~RUBY) | ||
foo, bar = foo, bar | ||
^^^^^^^^^^^^^^^^^^^ Self-assignment detected. | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using multiple var self-assignment through array' do | ||
expect_offense(<<~RUBY) | ||
foo, bar = [foo, bar] | ||
^^^^^^^^^^^^^^^^^^^^^ Self-assignment detected. | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using multiple var assignment' do | ||
expect_no_offenses(<<~RUBY) | ||
foo, bar = bar, foo | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using multiple var assignment through splat' do | ||
expect_no_offenses(<<~RUBY) | ||
foo, bar = *something | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using multiple var assignment through method call' do | ||
expect_no_offenses(<<~RUBY) | ||
foo, bar = something | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using shorthand-or var self-assignment' do | ||
expect_offense(<<~RUBY) | ||
foo ||= foo | ||
^^^^^^^^^^^ Self-assignment detected. | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using shorthand-or var assignment' do | ||
expect_no_offenses(<<~RUBY) | ||
foo ||= bar | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using shorthand-and var self-assignment' do | ||
expect_offense(<<~RUBY) | ||
foo &&= foo | ||
^^^^^^^^^^^ Self-assignment detected. | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using shorthand-and var assignment' do | ||
expect_no_offenses(<<~RUBY) | ||
foo &&= bar | ||
RUBY | ||
end | ||
end |