Skip to content

Lint/Void and Lint/UselessAssignment removed my code with side effect #12377

@stoivo

Description

@stoivo

In spec we had the following code

invoices = [FactoryBot.create(:invoice, customer_unit_id: @user.current_unit.id),
            FactoryBot.create(:invoice, supplier_unit_id: @user.current_unit.id),]
user = FactoryBot.create(:user)

During work to enable Lint/UselessAssignment. Rubocop found that invoices was an unused variable. I ran be rubocop path.rb --only Lint/UselessAssignment -A and it replaced the code with the following code. which is fine. Lint/UselessAssignment is unsafe autocorrect so I ran it with -A.

[FactoryBot.create(:invoice, customer_unit_id: @user.current_unit.id),
 FactoryBot.create(:invoice, supplier_unit_id: @user.current_unit.id),]
user = FactoryBot.create(:user)

When I lint this code I get errors from Lint/Void. Running autofix safe version (-a) rubocop removed the array definition, so my two invoices are not created any more.

user = FactoryBot.create(:user)

Expected behavior

My suggestion is to don't modify the array if it has anything which could have side effect

Actual behavior

Removes by factory bot calls

Steps to reproduce the problem

Explained above

RuboCop version

⋊> ~/d/w/d/f/repo on st-bugsnag-w40 ⨯ bundle exec rubocop -V                                                                                                                              12:57:13
1.57.2 (using Parser 3.2.2.4, rubocop-ast 1.30.0, running on ruby 3.2.1) [arm64-darwin22]
  - rubocop-capybara 2.19.0
  - rubocop-factory_bot 2.24.0
  - rubocop-performance 1.19.1
  - rubocop-rails 2.22.1
  - rubocop-rspec 2.25.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions