Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix #10011] Fix a false positive for Style/RedundantSelfAssignmentBranch #10012

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#10011](https://github.com/rubocop/rubocop/issues/10011): Fix a false positive for `Style/RedundantSelfAssignmentBranch` when using instance variable, class variable, and global variable. ([@koic][])
7 changes: 3 additions & 4 deletions lib/rubocop/cop/style/redundant_self_assignment_branch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ module Cop
module Style
# This cop checks for places where conditional branch makes redundant self-assignment.
#
# It only detects local variable because it may replace state of instance variable,
# class variable, and global variable that have state across methods with `nil`.
#
# @example
#
# # bad
Expand Down Expand Up @@ -45,10 +48,6 @@ def on_lvasgn(node)
end
end

alias on_ivasgn on_lvasgn
alias on_cvasgn on_lvasgn
alias on_gvasgn on_lvasgn

private

def self_assign?(variable, branch)
Expand Down
27 changes: 6 additions & 21 deletions spec/rubocop/cop/style/redundant_self_assignment_branch_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,36 +125,21 @@
RUBY
end

it 'registers and corrects an offense when self-assigning redundant else ternary branch for ivar' do
expect_offense(<<~RUBY)
it 'does not register an offense when self-assigning redundant else ternary branch for ivar' do
expect_no_offenses(<<~RUBY)
@foo = condition ? @bar : @foo
^^^^ Remove the self-assignment branch.
RUBY

expect_correction(<<~RUBY)
@foo = @bar if condition
RUBY
end

it 'registers and corrects an offense when self-assigning redundant else ternary branch for cvar' do
expect_offense(<<~RUBY)
it 'does not register an offense when self-assigning redundant else ternary branch for cvar' do
expect_no_offenses(<<~RUBY)
@@foo = condition ? @@bar : @@foo
^^^^^ Remove the self-assignment branch.
RUBY

expect_correction(<<~RUBY)
@@foo = @@bar if condition
RUBY
end

it 'registers and corrects an offense when self-assigning redundant else ternary branch for gvar' do
expect_offense(<<~RUBY)
it 'does not register an offense when self-assigning redundant else ternary branch for gvar' do
expect_no_offenses(<<~RUBY)
$foo = condition ? $bar : $foo
^^^^ Remove the self-assignment branch.
RUBY

expect_correction(<<~RUBY)
$foo = $bar if condition
RUBY
end

Expand Down