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

False positive for Lint/UselessAssignment when assigning in branch and block #13395

Open
Earlopain opened this issue Oct 27, 2024 · 3 comments · May be fixed by #13409
Open

False positive for Lint/UselessAssignment when assigning in branch and block #13395

Earlopain opened this issue Oct 27, 2024 · 3 comments · May be fixed by #13409

Comments

@Earlopain
Copy link
Contributor

Expected behavior

The following code warns about a unused variable:

changed = false

if Random.rand > 1
  changed = true
end

[].each do
  changed = true
end

puts changed

I expect no offense for this case. The first assignment is meaningful, removing it will change the output from false to nil. Changed with #13389, not released yet.

Actual behavior

$ bundle exec rubocop test.rb --only Lint/UselessAssignment
Inspecting 1 file
W

Offenses:

test.rb:1:1: W: [Correctable] Lint/UselessAssignment: Useless assignment to variable - changed.
changed = false
^^^^^^^

1 file inspected, 1 offense detected, 1 offense autocorrectable

RuboCop version

Commit 8454d933
$ bundle exec rubocop -V
1.67.0 (using Parser 3.3.5.0, rubocop-ast 1.32.3, analyzing as Ruby 2.7, running on ruby 3.3.5) [x86_64-linux]
  - rubocop-performance 1.22.1
  - rubocop-rake 0.6.0
  - rubocop-rspec 3.2.0
@monkeyWzr
Copy link

monkeyWzr commented Nov 1, 2024

Same situation with me. And autofix will change the function's first line from a = true to just true

def foo
  a = true # RuboCop: Useless assignment to variable - `a`. [Lint/UselessAssignment]
  if something
    a = false
  end
  [].each do |n|
    a = false
  end
  a
end

@koic
Copy link
Member

koic commented Nov 1, 2024

cc @pCosta99

@pCosta99
Copy link
Contributor

pCosta99 commented Nov 1, 2024

I'll take a look throughout the day if no one gets to it first

pCosta99 added a commit to pCosta99/rubocop that referenced this issue Nov 1, 2024
pCosta99 added a commit to pCosta99/rubocop that referenced this issue Nov 1, 2024
pCosta99 added a commit to pCosta99/rubocop that referenced this issue Nov 1, 2024
pCosta99 added a commit to pCosta99/rubocop that referenced this issue Nov 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants