Project

General

Profile

Bug #16020

Forbid `if` `elsif` without a condition

Added by bogdanvlviv (Bogdan Denkovych) 4 months ago. Updated 4 months ago.

Status:
Rejected
Priority:
Normal
Assignee:
-
Target version:
-
ruby -v:
ruby 2.6.3p62 (2019-04-16 revision 67580) [x86_64-linux]
[ruby-core:93908]

Description

Hello.

I might have missed something, but examples like:

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

# Output:
# !!!!1
# !!!!2
# !!!!3
# !!!!4
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

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

is similar to

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.

History

#1

Updated by bogdanvlviv (Bogdan Denkovych) 4 months ago

  • ruby -v changed from ruby 2.6.3p62 (2019-04-16 revision 67580) [x86_64-linux to ruby 2.6.3p62 (2019-04-16 revision 67580) [x86_64-linux]
#2

Updated by bogdanvlviv (Bogdan Denkovych) 4 months ago

  • Description updated (diff)
#3

Updated by bogdanvlviv (Bogdan Denkovych) 4 months ago

  • Description updated (diff)
#5

Updated by marcandre (Marc-Andre Lafortune) 4 months ago

  • Status changed from Open to Rejected

Also available in: Atom PDF