Skip to content

Lint/UselessAssignment: false positives with += #14419

@joshcooper

Description

@joshcooper

Lint/UselessAssignment reports false positives even after 1.79.1 and #14409

Expected behavior

rubocop should not report a violation

Actual behavior

Rubocop reports lines 8 and 12 are useless assignments, but not 16 for some reason. If I remove line 4, then the false positives are eliminated.

Steps to reproduce the problem

def parse_options
  index = -1
  while loop_cond
    index += 1

    item = command_line.args[index]
    if option
      index += 1
    else
      option = foo(item)
      if option
        index += 1
      else
        option = bar(item)
        if option
          index += 1
        end
      end
    end
  end
end
❯ git rev-parse HEAD
eb470cef93cacb1e8806c6202e976d5693f91b3d

❯ rm -rf /home/josh/.cache/rubocop_cache

❯ bundle exec rubocop --debug --only Lint/UselessAssignment example.rb
For /home/josh/work/rubocop: configuration from /home/josh/work/rubocop/.rubocop.yml
Default configuration from /home/josh/work/rubocop/config/default.yml
Plugin configuration from /home/josh/work/rubocop/config/internal_affairs.yml
Plugin configuration from /home/josh/.rbenv/versions/3.2.5/lib/ruby/gems/3.2.0/gems/rubocop-performance-1.25.0/config/default.yml
Plugin configuration from /home/josh/.rbenv/versions/3.2.5/lib/ruby/gems/3.2.0/gems/rubocop-rake-0.7.1/config/default.yml
Plugin configuration from /home/josh/.rbenv/versions/3.2.5/lib/ruby/gems/3.2.0/gems/rubocop-rspec-3.6.0/config/default.yml
Inheriting configuration from /home/josh/work/rubocop/.rubocop_todo.yml
Use parallel by default.
Skipping parallel inspection: only a single file needs inspection
Inspecting 1 file
Scanning /home/josh/work/rubocop/example.rb
W

Offenses:

example.rb:8:7: W: [Correctable] Lint/UselessAssignment: Useless assignment to variable - index.
      index += 1
      ^^^^^
example.rb:12:9: W: [Correctable] Lint/UselessAssignment: Useless assignment to variable - index.
        index += 1
        ^^^^^

1 file inspected, 2 offenses detected, 2 offenses autocorrectable
Finished in 0.25948607735335827 seconds

RuboCop version

❯ bundle exec rubocop -V 
1.79.1 (using Parser 3.3.9.0, rubocop-ast 1.46.0, analyzing as Ruby 2.7, running on ruby 3.2.5) [x86_64-linux]
  - rubocop-performance 1.25.0
  - rubocop-rake 0.7.1
  - rubocop-rspec 3.6.0

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