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

Fix Lint/UselessAssignment false positive #11510

Merged
merged 1 commit into from
May 10, 2023

Conversation

sambostock
Copy link
Contributor

A variable assigned in a block is not unused, if it was captured from the surrounding scope, where it is later used.

var = nil
do_something { |arg| var = arg }
use(var)

We already cover this case for normal blocks, but missed the edge case where the block uses numbered variables (i.e. numblock vs block).

var = nil
do_something { var = _1 }
             # ^^^ False positive for useless assignment
use(var)

This fixes the false positive.


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.

@@ -113,14 +113,14 @@ def variable_exist?(name)
def accessible_variables
scope_stack.reverse_each.with_object([]) do |scope, variables|
variables.concat(scope.variables.values)
break variables unless scope.node.block_type?
break variables unless scope.node.block_type? || scope.node.numblock_type?
Copy link
Contributor Author

Choose a reason for hiding this comment

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

As mentioned in the commit message, I tentatively added this to match the other block_type? call sites in this file:

I don't know what the false positive is here, but I'm assuming there is one. If someone with more context knows what it might be, I'm can add a corresponding test and update the commit message and changelog.

Comment on lines 1340 to 1373
it 'registers an offense when the variable is assigned and but unused' do
expect_offense(<<~RUBY)
var = nil
^^^ Useless assignment to variable - `var`.

do_something { var = _1 }
^^^ Useless assignment to variable - `var`.
RUBY
end
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm... I just added this test as the inverse of the one above, but it looks like it fails. I think it's because

  • var = nil is considered fine, because a block later uses it
  • var = _1 is considered fine, because it's assigning to a shadowed variable

I don't think there's anything checking that the variable is used in neither the block, nor the surrounding scope.

I'm not sure how to go about implementing that, so I may just remove the test and accept the unlikely false negative. 🤷

Copy link
Member

Choose a reason for hiding this comment

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

Yep. It's an existing issue that also happens with normal block (not numbered block). So, can you remove this test?

@bbatsov bbatsov requested a review from koic February 3, 2023 06:24
@bbatsov
Copy link
Collaborator

bbatsov commented Feb 26, 2023

@sambostock The CI is not passing currently.

@@ -0,0 +1 @@
* [#11510](https://github.com/rubocop/rubocop/pull/11510): Tentatively check against `numblock_type?` too. ([@sambostock][])
Copy link
Member

Choose a reason for hiding this comment

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

Can you remove this changelog entry?

@koic
Copy link
Member

koic commented May 6, 2023

I left a comment about removing 2 places. Please squash your commits into one after the remove.

@sambostock
Copy link
Contributor Author

@koic Sorry for the delay; updated. 👍

@koic
Copy link
Member

koic commented May 10, 2023

@sambostock Finally noticed about the changelog entry. Can you take a look?

A variable assigned in a block is not unused, if it was captured from
the surrounding scope, where it is later used.

    var = nil
    do_something { |arg| var = arg }
    use(var)

We already cover this case for normal blocks, but missed the edge case
where the block uses numbered variables (i.e. `numblock` vs `block`).

    var = nil
    do_something { var = _1 }
    use(var)
@sambostock sambostock requested a review from koic May 10, 2023 12:56
@koic koic merged commit ec970d7 into rubocop:master May 10, 2023
27 of 28 checks passed
@koic
Copy link
Member

koic commented May 10, 2023

Thanks!

@sambostock sambostock deleted the useless-assignment-bug branch May 10, 2023 16:19
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.

None yet

3 participants