From 4addf0987af65c8129d2b19c0385de02bb01f044 Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Fri, 13 Aug 2021 20:21:23 +0900 Subject: [PATCH] [Fix #10011] Fix a false positive for `Style/RedundantSelfAssignmentBranch` Fixes #10011. This PR fixes a false positive for `Style/RedundantSelfAssignmentBranch` when using instance variable, class variable, and global variable. --- ..._style_redundant_self_assignment_branch.md | 1 + .../style/redundant_self_assignment_branch.rb | 7 +++-- .../redundant_self_assignment_branch_spec.rb | 27 +++++-------------- 3 files changed, 10 insertions(+), 25 deletions(-) create mode 100644 changelog/fix_false_positive_for_style_redundant_self_assignment_branch.md diff --git a/changelog/fix_false_positive_for_style_redundant_self_assignment_branch.md b/changelog/fix_false_positive_for_style_redundant_self_assignment_branch.md new file mode 100644 index 00000000000..a3c599b1f04 --- /dev/null +++ b/changelog/fix_false_positive_for_style_redundant_self_assignment_branch.md @@ -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][]) diff --git a/lib/rubocop/cop/style/redundant_self_assignment_branch.rb b/lib/rubocop/cop/style/redundant_self_assignment_branch.rb index c9083cd3178..d8a9dd8c670 100644 --- a/lib/rubocop/cop/style/redundant_self_assignment_branch.rb +++ b/lib/rubocop/cop/style/redundant_self_assignment_branch.rb @@ -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 @@ -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) diff --git a/spec/rubocop/cop/style/redundant_self_assignment_branch_spec.rb b/spec/rubocop/cop/style/redundant_self_assignment_branch_spec.rb index 09e8955bc9f..bc88cc7050e 100644 --- a/spec/rubocop/cop/style/redundant_self_assignment_branch_spec.rb +++ b/spec/rubocop/cop/style/redundant_self_assignment_branch_spec.rb @@ -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