Skip to content

Lint/UselessAssignment false positive for loop with conditional #14383

@viralpraxis

Description

@viralpraxis
def f
  string = "a"
  last_c = false
  while string.size < 11
    if string[-1] == 'c'
      string += "b"
      last_c = false
    else
      string += "c"
      last_c = true
    end
  end

  last_c
end

Expected behavior

No Lint/UselessAssignment offenses

Actual behavior

test.rb:5:7: W: [Correctable] Lint/UselessAssignment: Useless assignment to variable - string. (https://rubystyle.guide#underscore-unused-vars)
      string += "b"

Steps to reproduce the problem

Run provided code on latest RuboCop version.

Likely caused by #14377

RuboCop version

1.79.0 (using Parser 3.3.8.0, Prism 1.4.0, parser_prism, rubocop-ast 1.46.0, analyzing as Ruby 3.4, running on ruby 3.4.4) [x86_64-linux]

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions