Skip to content

Fix false negatives in Lint/Void when using ensure, defs and numblock #13131

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

Merged
merged 1 commit into from
Aug 18, 2024

Conversation

vlad-pisanov
Copy link
Contributor

@vlad-pisanov vlad-pisanov commented Aug 17, 2024

Improve Lint/Void to detect more void contexts:

  1. Literals returned from ensure: the return value of ensure is always discarded. This is a tripping point for many devs including myself (one might easily assume that ensure behaves just like rescue and that the "ensured" value is returned.)
def foo
  bar
ensure
  baz
  42 # ⚠️ new offense: Literal `42` used in void context.
end
  1. Handle void contexts in class method definitions (defs) and numbered parameter blocks (numblock):
def self.foo=(value)
  42 # ⚠️ new offense: Literal `42` used in void context.
end
foo.tap do
  _1 # ⚠️ new offense: Variable `_1` used in void context.
end

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.

Copy link
Contributor

@Earlopain Earlopain left a comment

Choose a reason for hiding this comment

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

👍 even ruby gets this wrong https://bugs.ruby-lang.org/issues/20680

def in_void_context?(node)
parent = node.parent

return false unless parent && parent.children.last == node

VOID_CONTEXT_TYPES.include?(parent.type) && parent.void_context?
ALWAYS_VOID_CONTEXT_TYPES.include?(parent.type) ||
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's wait on rubocop/rubocop-ast#309 (if merged) after which this is a very simple change:

Suggested change
ALWAYS_VOID_CONTEXT_TYPES.include?(parent.type) ||
parent.respond_to?(:void_context?) && parent.void_context?

No need for other changes, and also exposes a small issue with VOID_CONTEXT_TYPES not including numblock

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Haha, perfect timing on that PR 💯

Copy link
Contributor

Choose a reason for hiding this comment

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

And already released. You can just bump the rubocop-ast version in the gemspec to pull that in.

@@ -819,4 +819,40 @@ def foo
end
RUBY
end

it 'registers an offense when literal is the last line of `ensure`' do
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's copy tests that already concern themselves with the same thing and move to the same location, just search for registers two offenses for void literals.

@@ -102,7 +106,6 @@ def check_begin(node)
expressions.each do |expr|
check_void_op(expr) do
block_node = node.each_ancestor(:block).first

Copy link
Member

Choose a reason for hiding this comment

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

Please leave this blank line.

@@ -38,7 +38,7 @@ Gem::Specification.new do |s|
s.add_dependency('rainbow', '>= 2.2.2', '< 4.0')
s.add_dependency('regexp_parser', '>= 2.4', '< 3.0')
s.add_dependency('rexml', '>= 3.2.5', '< 4.0')
s.add_dependency('rubocop-ast', '>= 1.32.0', '< 2.0')
s.add_dependency('rubocop-ast', '>= 1.32.1', '< 2.0')
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@vlad-pisanov vlad-pisanov changed the title Fix a false negative in Lint/Void when using ensure Fix false negatives in Lint/Void when using ensure, defs and numblock Aug 17, 2024
@koic koic merged commit e30f542 into rubocop:master Aug 18, 2024
22 checks passed
@koic
Copy link
Member

koic commented Aug 18, 2024

Thanks!

Earlopain added a commit to Earlopain/rubocop that referenced this pull request Feb 1, 2025
…ional expressions that has multiple statements

Followup rubocop#12688

Also remove a different block check that I don't think is doing anything.
It was added in rubocop#13131 but all tests still pass
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.

3 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy