-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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
Conversation
d51a609
to
c6dca4d
Compare
There was a problem hiding this 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
lib/rubocop/cop/lint/void.rb
Outdated
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) || |
There was a problem hiding this comment.
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:
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
There was a problem hiding this comment.
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 💯
There was a problem hiding this comment.
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.
spec/rubocop/cop/lint/void_spec.rb
Outdated
@@ -819,4 +819,40 @@ def foo | |||
end | |||
RUBY | |||
end | |||
|
|||
it 'registers an offense when literal is the last line of `ensure`' do |
There was a problem hiding this comment.
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
.
lib/rubocop/cop/lint/void.rb
Outdated
@@ -102,7 +106,6 @@ def check_begin(node) | |||
expressions.each do |expr| | |||
check_void_op(expr) do | |||
block_node = node.each_ancestor(:block).first | |||
|
There was a problem hiding this comment.
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.
c6dca4d
to
0187c19
Compare
@@ -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') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Needed for rubocop/rubocop-ast#309
Lint/Void
when using ensure
Lint/Void
when using ensure
, defs
and numblock
Thanks! |
…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
Improve
Lint/Void
to detect more void contexts:ensure
: the return value ofensure
is always discarded. This is a tripping point for many devs including myself (one might easily assume thatensure
behaves just likerescue
and that the "ensured" value is returned.)defs
) and numbered parameter blocks (numblock
):Before submitting the PR make sure the following are checked:
[Fix #issue-number]
(if the related issue exists).master
(if not - rebase it).bundle exec rake default
. It executes all tests and runs RuboCop on its own code.{change_type}_{change_description}.md
if the new code introduces user-observable changes. See changelog entry format for details.