[Bug #19919] Warn class variable assignment and constant declaration in condition

This commit is contained in:
yui-knk 2023-10-10 21:06:54 +09:00 коммит произвёл Yuichiro Kaneko
Родитель dcee3cc6ce
Коммит 2794a8fef6
2 изменённых файлов: 73 добавлений и 0 удалений

Просмотреть файл

@ -1835,6 +1835,10 @@ get_nd_value(struct parser_params *p, NODE *node)
return RNODE_DASGN(node)->nd_value;
case NODE_MASGN:
return RNODE_MASGN(node)->nd_value;
case NODE_CVASGN:
return RNODE_CVASGN(node)->nd_value;
case NODE_CDECL:
return RNODE_CDECL(node)->nd_value;
default:
compile_error(p, "unexpected node: %s", parser_node_name(nd_type(node)));
return 0;
@ -14044,6 +14048,8 @@ assign_in_cond(struct parser_params *p, NODE *node)
case NODE_DASGN:
case NODE_GASGN:
case NODE_IASGN:
case NODE_CVASGN:
case NODE_CDECL:
break;
default:

Просмотреть файл

@ -895,12 +895,14 @@ x = __ENCODING__
end
def test_assign_in_conditional
# multiple assignment
assert_warning(/`= literal' in conditional/) do
eval <<-END, nil, __FILE__, __LINE__+1
(x, y = 1, 2) ? 1 : 2
END
end
# instance variable assignment
assert_warning(/`= literal' in conditional/) do
eval <<-END, nil, __FILE__, __LINE__+1
if @x = true
@ -910,6 +912,71 @@ x = __ENCODING__
end
END
end
# local variable assignment
assert_warning(/`= literal' in conditional/) do
eval <<-END, nil, __FILE__, __LINE__+1
def m
if x = true
1
else
2
end
end
END
end
# global variable assignment
assert_separately([], <<-RUBY)
assert_warning(/`= literal' in conditional/) do
eval <<-END, nil, __FILE__, __LINE__+1
if $x = true
1
else
2
end
END
end
RUBY
# dynamic variable assignment
assert_warning(/`= literal' in conditional/) do
eval <<-END, nil, __FILE__, __LINE__+1
y = 1
1.times do
if y = true
1
else
2
end
end
END
end
# class variable assignment
assert_warning(/`= literal' in conditional/) do
eval <<-END, nil, __FILE__, __LINE__+1
c = Class.new
class << c
if @@a = 1
end
end
END
end
# constant declaration
assert_separately([], <<-RUBY)
assert_warning(/`= literal' in conditional/) do
eval <<-END, nil, __FILE__, __LINE__+1
if Const = true
1
else
2
end
END
end
RUBY
end
def test_literal_in_conditional