-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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
Make Style/RedundantBegin aware of do-end block in Ruby 2.5 #5175
Conversation
d3c69b9
to
95dba4d
Compare
private | ||
|
||
def check(node) | ||
return unless node.body && node.body.kwbegin_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.
I think the cop is not accounting for the case where there are additional expressions before the begin
. In those cases the body
is wrapped in a begin
node. Consider:
foo do
bar
begin
baz
rescue
qux
end
end
Which will result in the following AST:
(block
(send nil :foo)
(args)
(begin
(send nil :bar)
(kwbegin
(rescue
(send nil :baz)
(resbody nil nil
(send nil :qux)) nil))))
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.
There's a difference between begin
and kwbegin
. The code seems correct to me, although adding an extra test case probably won't hurt.
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.
The problem is in how parser handles most bodies. If the body a single expression, it omits wrapping it in a begin
. So if we want to check for offenses in both single- and multiple expression blocks, we need to do something like:
if body.offending_type? || body.begin_type? && body.children.any?(&:offending_type?)
The code above won't register an offense currently, even though it can be changed to:
foo do
bar
baz
rescue
qux
end
in Ruby 2.5.
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.
The code above won't register an offense currently, even though it can be changed to:
I think the changing is not safe. The code has different behaviour when bar
raises an error.
In some cases the changing is safe, but in other cases the changing is not safe.
For example:
# In this case, omitting `begin` and `end` is safe.
# Because `Repository.find` (`ActiveRecord method`) never raise `Octokit` error.
foo do
repository = Repository.find(params[:id])
begin
Octokit::Client.new.repository(repository.full_name)
rescue Octokit::NotFound
handle_error
end
end
# In this case, omitting `begin` and `end` is not safe.
# Because `Repository.find` may raise `ActiveRecord::RecordNotFound` error.
foo do
repository = Repository.find(params[:id])
begin
user = User.find(params[:user_id])
rescue ActiveRecord::RecordNotFound
handle_error
end
end
It depends on an exception and methods, so I think this cop should not add any offense for the code.
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.
Nice enhancement in Ruby, and nice implementation by you! 🚀
@@ -153,4 +153,31 @@ def method | |||
new_source = autocorrect_source(src) | |||
expect(new_source).to eq(result_src) | |||
end | |||
|
|||
context '<= ruby 2.5', :ruby25 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.
Maybe '>= ruby 2.5'
?
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.
True. I'll fix it soon. Thanks!
e968530
to
5bbdfdd
Compare
We can omit `begin` in `do-end` block since Ruby 2.5. https://bugs.ruby-lang.org/projects/ruby-trunk/repository/revisions/57376 For example, this code is correct in Ruby 2.5. ```ruby do_something do something rescue => ex anything end ``` This change will make Style/RedundantBegin aware of do-end block in Ruby 2.5.
I added test cases that are mentioned in the comment ( #5175 (comment) ), and rebased. |
We can omit
begin
indo-end
block since Ruby 2.5.https://bugs.ruby-lang.org/projects/ruby-trunk/repository/revisions/57376
For example, this code is correct in Ruby 2.5.
This change will make Style/RedundantBegin aware of do-end block in Ruby 2.5.
Before submitting the PR make sure the following are checked:
[Fix #issue-number]
(if the related issue exists).master
(if not - rebase it).rake spec
) are passing.rake internal_investigation
.and description in grammatically correct, complete sentences.
rake generate_cops_documentation
(required only when you've added a new cop or changed the configuration/documentation of an existing cop).