ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-core:93908] [Ruby master Bug#16020] Forbid `if` `elsif` without a condition
       [not found] <redmine.issue-16020.20190725121647@ruby-lang.org>
@ 2019-07-25 12:16 ` bogdanvlviv
  2019-07-25 13:46   ` [ruby-core:93909] " Austin Ziegler
  2019-07-25 13:56 ` [ruby-core:93910] " zn
  1 sibling, 1 reply; 3+ messages in thread
From: bogdanvlviv @ 2019-07-25 12:16 UTC (permalink / raw
  To: ruby-core

Issue #16020 has been reported by bogdanvlviv (Bogdan Denkovych).

----------------------------------------
Bug #16020: Forbid `if` `elsif` without a condition
https://bugs.ruby-lang.org/issues/16020

* Author: bogdanvlviv (Bogdan Denkovych)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
* ruby -v: ruby 2.6.3p62 (2019-04-16 revision 67580) [x86_64-linux
* Backport: 2.5: UNKNOWN, 2.6: UNKNOWN
----------------------------------------
Hello.

I might have missed something, but examples like:
```ruby
if
  puts "!!!!1"
elsif
  puts "!!!!2"
elsif
  puts "!!!!3"
else
  puts "!!!!4"
end

# Output:
# !!!!1
# !!!!2
# !!!!3
# !!!!4
```

```ruby
if false
  puts "!!!!1"
elsif 1==2
  puts "!!!!2"
elsif
  puts "!!!!3"
else
  puts "!!!!4"
end

# Output:
# !!!!3
# !!!!4
```

are confusing. We probably should raise `SyntaxError` in the case when `if`/`elsif` is being used without any condition. What do you think about it?

Original source: https://twitter.com/bogdanvlviv/status/1154356514628493313



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

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [ruby-core:93909] Re: [Ruby master Bug#16020] Forbid `if` `elsif` without a condition
  2019-07-25 12:16 ` [ruby-core:93908] [Ruby master Bug#16020] Forbid `if` `elsif` without a condition bogdanvlviv
@ 2019-07-25 13:46   ` Austin Ziegler
  0 siblings, 0 replies; 3+ messages in thread
From: Austin Ziegler @ 2019-07-25 13:46 UTC (permalink / raw
  To: Ruby developers


[-- Attachment #1.1: Type: text/plain, Size: 2311 bytes --]

This is not a syntax error, but a logic error. Ruby is not line-oriented,
but expression oriented. The parser knows that `if` has to have a
condition, so if it does not find one in the next token, it looks for the
next expression.

This is what your code _actually_ got parsed as:

```ruby
if puts "!!!!1"
elsif puts "!!!!2"
elsif puts "!!!!3"
else
  puts "!!!!4"
end
```

`Kernel#puts` returns `nil`, which is falsy and imperative, so the `puts`
for 1, 2, and 3 must execute and because all of them are false, the puts
for 4 executes.

This is clearer if you add something else:

```ruby
if puts "!!!!1"
  puts 1
elsif puts "!!!!2"
  puts 2
elsif puts "!!!!3"
  puts 3
else
  puts "!!!!4"
end
```

-a

On Thu, Jul 25, 2019 at 8:17 AM <bogdanvlviv@gmail.com> wrote:

> Issue #16020 has been reported by bogdanvlviv (Bogdan Denkovych).
>
> ----------------------------------------
> Bug #16020: Forbid `if` `elsif` without a condition
> https://bugs.ruby-lang.org/issues/16020
>
> * Author: bogdanvlviv (Bogdan Denkovych)
> * Status: Open
> * Priority: Normal
> * Assignee:
> * Target version:
> * ruby -v: ruby 2.6.3p62 (2019-04-16 revision 67580) [x86_64-linux
> * Backport: 2.5: UNKNOWN, 2.6: UNKNOWN
> ----------------------------------------
> Hello.
>
> I might have missed something, but examples like:
> ```ruby
> if
>   puts "!!!!1"
> elsif
>   puts "!!!!2"
> elsif
>   puts "!!!!3"
> else
>   puts "!!!!4"
> end
>
> # Output:
> # !!!!1
> # !!!!2
> # !!!!3
> # !!!!4
> ```
>
> ```ruby
> if false
>   puts "!!!!1"
> elsif 1==2
>   puts "!!!!2"
> elsif
>   puts "!!!!3"
> else
>   puts "!!!!4"
> end
>
> # Output:
> # !!!!3
> # !!!!4
> ```
>
> are confusing. We probably should raise `SyntaxError` in the case when
> `if`/`elsif` is being used without any condition. What do you think about
> it?
>
> Original source:
> https://twitter.com/bogdanvlviv/status/1154356514628493313
>
>
>
> --
> https://bugs.ruby-lang.org/
>
> Unsubscribe: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe>
> <http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>
>


-- 
Austin Ziegler • halostatue@gmail.com • austin@halostatue.ca
http://www.halostatue.ca/http://twitter.com/halostatue

[-- Attachment #1.2: Type: text/html, Size: 4037 bytes --]

[-- Attachment #2: Type: text/plain, Size: 0 bytes --]



^ permalink raw reply	[flat|nested] 3+ messages in thread

* [ruby-core:93910] [Ruby master Bug#16020] Forbid `if` `elsif` without a condition
       [not found] <redmine.issue-16020.20190725121647@ruby-lang.org>
  2019-07-25 12:16 ` [ruby-core:93908] [Ruby master Bug#16020] Forbid `if` `elsif` without a condition bogdanvlviv
@ 2019-07-25 13:56 ` zn
  1 sibling, 0 replies; 3+ messages in thread
From: zn @ 2019-07-25 13:56 UTC (permalink / raw
  To: ruby-core

Issue #16020 has been updated by znz (Kazuhiro NISHIYAMA).


Rubocop can detect such bug.

<https://rubocop.readthedocs.io/en/stable/cops_layout/#layoutconditionposition>

----------------------------------------
Bug #16020: Forbid `if` `elsif` without a condition
https://bugs.ruby-lang.org/issues/16020#change-80001

* Author: bogdanvlviv (Bogdan Denkovych)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
* ruby -v: ruby 2.6.3p62 (2019-04-16 revision 67580) [x86_64-linux]
* Backport: 2.5: UNKNOWN, 2.6: UNKNOWN
----------------------------------------
Hello.

I might have missed something, but examples like:
```ruby
if
  puts "!!!!1"
elsif
  puts "!!!!2"
elsif
  puts "!!!!3"
else
  puts "!!!!4"
end

# Output:
# !!!!1
# !!!!2
# !!!!3
# !!!!4
```

```ruby
if false
  puts "!!!!1"
elsif 1==2
  puts "!!!!2"
elsif
  puts "!!!!3"
else
  puts "!!!!4"
end

# Output:
# !!!!3
# !!!!4
```

are confusing. We probably should raise `SyntaxError` in the case when `if`/`elsif` is being used without any condition. What do you think about it?

Original source: https://twitter.com/bogdanvlviv/status/1154356514628493313

Edited:

https://twitter.com/mamantoha/status/1154369189647454213 helped me to figure out that

The code like

```ruby
if false
  puts "!!!!1"
elsif 1==2
  puts "!!!!2"
elsif
  puts "!!!!3"
else
  puts "!!!!4"
end
```
is similar to
```ruby
if false
  puts "!!!!1"
elsif 1==2
  puts "!!!!2"
elsif (puts "!!!!3")
else (puts "!!!!4")
end
```

Probably Ruby should be more strict in those cases because such code is more prone to bugs.



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

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2019-07-25 13:56 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <redmine.issue-16020.20190725121647@ruby-lang.org>
2019-07-25 12:16 ` [ruby-core:93908] [Ruby master Bug#16020] Forbid `if` `elsif` without a condition bogdanvlviv
2019-07-25 13:46   ` [ruby-core:93909] " Austin Ziegler
2019-07-25 13:56 ` [ruby-core:93910] " zn

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).