Skip to content

Commit 01bf777

Browse files
authored
Merge pull request #545 from Shopify/catlee/no_return_in_blocks
Enable the Lint/NoReturnInBeginEndBlocks cop
2 parents 5c936af + 326dbbc commit 01bf777

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,31 @@ documentation](https://docs.rubocop.org/rubocop/configuration.html#inheriting-co
625625
* Prefer `Time.iso8601(foo)` instead of `Time.parse(foo)` when expecting ISO8601
626626
formatted time strings like `"2018-03-20T11:16:39-04:00"`.
627627

628+
* Avoid returning from a `begin` block in assignment contexts. If you return
629+
from a method inside a `begin` block, the return will prevent the assignment
630+
from taking place, potentially causing confusing memoization bugs.
631+
632+
~~~ ruby
633+
# bad
634+
def foo
635+
@foo ||= begin
636+
return 1 if flag?
637+
2
638+
end
639+
end
640+
641+
# good
642+
def foo
643+
@foo ||= begin
644+
if flag?
645+
1
646+
else
647+
2
648+
end
649+
end
650+
end
651+
~~~
652+
628653
## Naming
629654

630655
* Use `snake_case` for symbols, methods, and variables.

rubocop.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ Lint/NestedPercentLiteral:
195195
Enabled: false
196196

197197
Lint/NoReturnInBeginEndBlocks:
198-
Enabled: false
198+
Enabled: true
199199

200200
Lint/NonAtomicFileOperation:
201201
Enabled: false

test/fixtures/full_config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1464,7 +1464,7 @@ Lint/NextWithoutAccumulator:
14641464
VersionAdded: '0.36'
14651465
Lint/NoReturnInBeginEndBlocks:
14661466
Description: Do not `return` inside `begin..end` blocks in assignment contexts.
1467-
Enabled: false
1467+
Enabled: true
14681468
VersionAdded: '1.2'
14691469
Lint/NonAtomicFileOperation:
14701470
Description: Checks for non-atomic file operations.

0 commit comments

Comments
 (0)