Issue #11143 has been updated by bug hit.


Nobuyoshi Nakada wrote:

> 
> > Just about everything in ruby is an expression and all expressions can potentially fail to produce a value (due to some jump: raise, throw, return, etc).  That does not make such expressions syntactically invalid (e.g. `a = (1; return; 2)`), except for the if?  Why?
> 
> Thank you, it's a bug.
> That `return` should be invalid too.

There is nothing special about return, there are other jumps (return, break, next, redo, retry, throw, catch)

For your proposal to make sense, you would have to detect every possible jump in every possible expression whose value is not discarded, and produce a syntax error.  This is crazy.  You still have not stated why that's a good idea.  AFAICT there is nothing wrong and in many cases useful to be able to jump out of an expression when conditions warrant, especially because in ruby just about everything is an expression.

----------------------------------------
Bug #11143: it should always be possible to return from an if
https://bugs.ruby-lang.org/issues/11143#change-52487

* Author: bug hit
* Status: Open
* Priority: Normal
* Assignee: 
* ruby -v: 
* Backport: 2.0.0: UNKNOWN, 2.1: UNKNOWN, 2.2: UNKNOWN
----------------------------------------
```
irb(main):001:0> def foo; a = if true then return end end
SyntaxError: (irb):1: void value expression
```

it should not matter that you are not producing a value for the if expression, since you are leaving the method immediately.  This should also apply to other jumps.



-- 
https://bugs.ruby-lang.org/