KEMBAR78
Fix a false positive for Lint/UselessAssignment by 5hun-s · Pull Request #14377 · rubocop/rubocop · GitHub
Skip to content

Conversation

@5hun-s
Copy link

@5hun-s 5hun-s commented Jul 21, 2025

Fixes rubocop/rubocop-jp#15. (This link is Japanese issue)

This PR fixes a false positive for Lint/UselessAssignment when the assignment is inside a loop body.


Before submitting the PR make sure the following are checked:

  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Ran bundle exec rake default. It executes all tests and runs RuboCop on its own code.
  • Added an entry (file) to the changelog folder named {change_type}_{change_description}.md if the new code introduces user-observable changes. See changelog entry format for details.

@5hun-s 5hun-s changed the title [Fix #15] Fix a false positive for Lint/UselessAssignment Fix a false positive for Lint/UselessAssignment Jul 21, 2025
@koic
Copy link
Member

koic commented Jul 21, 2025

Can you rebase with the latest master branch?

@5hun-s 5hun-s force-pushed the fix_false_negative_for_lint_useless_assignment branch from 7011783 to 7ee22fb Compare July 21, 2025 04:01
@5hun-s
Copy link
Author

5hun-s commented Jul 21, 2025

I rebased on the latest master branch and corrected my commit history.

RUBY
end
end
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could there be false positives with case-match?

Suggested change
end
end
context 'when duplicate assignments in a case-match branch inside a loop', :ruby27 do
context 'while loop' do
it 'does not register an offense' do
expect_no_offenses(<<~RUBY)
while
case expr
in fizz then foo += 1
in buzz then foo -= 1
end
end
RUBY
end
end
end

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, it's necessary as well. I've added it. Thank you.

def reference_assignments(loop_assignments, node)
# If inside a case statement, mark all as referenced.
# Otherwise, mark only the last assignment as referenced.
if loop_assignments.first.node.each_ancestor(:case).any?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it necessary to account for case-match?

Suggested change
if loop_assignments.first.node.each_ancestor(:case).any?
if loop_assignments.first.node.each_ancestor(:case, :case_match).any?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, it's necessary as well. I've added it. Thank you.

@5hun-s 5hun-s force-pushed the fix_false_negative_for_lint_useless_assignment branch from 7ee22fb to bfe8ff3 Compare July 21, 2025 21:37
@koic koic merged commit 2013dd6 into rubocop:master Jul 21, 2025
24 checks passed
@koic
Copy link
Member

koic commented Jul 21, 2025

Thanks!

@5hun-s 5hun-s deleted the fix_false_negative_for_lint_useless_assignment branch July 21, 2025 22:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

while and UselessAssignment

2 participants