Skip to content

Commit 272771b

Browse files
koicbbatsov
authored andcommitted
[Fix #8871] Fix a false positive for Style/RedundantBegin
Fixes #8871. This PR fixes a false positive for `Style/RedundantBegin` when using `begin` for method argument or part of conditions. `begin` keyword may be redundant when using only one expression in `begin` of each issue case. However, since it is the unintended case for #8822, I think it can be implemented as an enhancement different from this bug fix.
1 parent f488821 commit 272771b

File tree

3 files changed

+42
-4
lines changed

3 files changed

+42
-4
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
* [#8869](https://github.com/rubocop-hq/rubocop/issues/8869): Fix a false positive for `Style/RedundantBegin` when using `begin` for or assignment and method call. ([@koic][])
1010
* [#8862](https://github.com/rubocop-hq/rubocop/issues/8862): Fix an error for `Lint/AmbiguousRegexpLiteral` when using regexp without method calls in nested structure. ([@koic][])
1111
* [#8872](https://github.com/rubocop-hq/rubocop/issues/8872): Fix an error for `Metrics/ClassLength` when multiple assignments to constants. ([@koic][])
12+
* [#8871](https://github.com/rubocop-hq/rubocop/issues/8871): Fix a false positive for `Style/RedundantBegin` when using `begin` for method argument or part of conditions. ([@koic][])
1213

1314
## 0.93.0 (2020-10-08)
1415

lib/rubocop/cop/style/redundant_begin.rb

+14-4
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,7 @@ def on_block(node)
8585
end
8686

8787
def on_kwbegin(node)
88-
return if node.each_ancestor.any?(&:assignment?) || node.parent&.post_condition_loop?
89-
90-
first_child = node.children.first
91-
return if first_child.rescue_type? || first_child.ensure_type?
88+
return if contain_rescue_or_ensure?(node) || valid_context_using_only_begin?(node)
9289

9390
register_offense(node)
9491
end
@@ -101,6 +98,19 @@ def register_offense(node)
10198
corrector.remove(node.loc.end)
10299
end
103100
end
101+
102+
def contain_rescue_or_ensure?(node)
103+
first_child = node.children.first
104+
105+
first_child.rescue_type? || first_child.ensure_type?
106+
end
107+
108+
def valid_context_using_only_begin?(node)
109+
parent = node.parent
110+
111+
node.each_ancestor.any?(&:assignment?) || parent&.post_condition_loop? ||
112+
parent&.send_type? || parent&.operator_keyword?
113+
end
104114
end
105115
end
106116
end

spec/rubocop/cop/style/redundant_begin_spec.rb

+27
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,33 @@ def method
233233
RUBY
234234
end
235235

236+
it 'does not register an offense when using `begin` for method argument' do
237+
expect_no_offenses(<<~RUBY)
238+
do_something begin
239+
foo
240+
bar
241+
end
242+
RUBY
243+
end
244+
245+
it 'does not register an offense when using `begin` for logical operator conditions' do
246+
expect_no_offenses(<<~RUBY)
247+
condition && begin
248+
foo
249+
bar
250+
end
251+
RUBY
252+
end
253+
254+
it 'does not register an offense when using `begin` for semantic operator conditions' do
255+
expect_no_offenses(<<~RUBY)
256+
condition and begin
257+
foo
258+
bar
259+
end
260+
RUBY
261+
end
262+
236263
context '< Ruby 2.5', :ruby24 do
237264
it 'accepts a do-end block with a begin-end' do
238265
expect_no_offenses(<<~RUBY)

0 commit comments

Comments
 (0)