Project

General

Profile

Actions

Bug #19919

closed

Variable assignments in condition are warned however class variable assignment and constant declaration are not warned

Added by yui-knk (Kaneko Yuichiro) about 1 year ago. Updated about 1 year ago.

Status:
Closed
Assignee:
-
Target version:
-
[ruby-core:115003]

Description

Is it better to warn for class variable assignment and constant declaration in condition?

(x, y = 1, 2) ? x : y # test.rb:1: warning: found `= literal' in conditional, should be ==

if a = 1 # test.rb:3: warning: found `= literal' in conditional, should be ==
end

b = 1
1.times do
  if b = 2 # test.rb:8: warning: found `= literal' in conditional, should be ==
  end
end

if $a = 1 # test.rb:12: warning: found `= literal' in conditional, should be ==
end

if @a = 1 # test.rb:15: warning: found `= literal' in conditional, should be ==
end

class A
  if @@a = 1 # no warning
  end
end

if B = 1 # no warning
end

I use ruby 3.3.0preview2 (2023-09-14 master e50fcca9a7).

Actions

Also available in: Atom PDF

Like1
Like0Like0